日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PostgreSQL MySQL 兼容性之 - bit 函数和操作符

發布時間:2025/1/21 数据库 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PostgreSQL MySQL 兼容性之 - bit 函数和操作符 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

bit 函數和操作符

MySQL

&Bitwise AND <<Left shift >>Shift right BIT_COUNTReturns the number of set bits ^Bitwise XOR |Bitwise OR ~Bitwise NOT

PostgreSQL

OperatorDescriptionExampleResult
||concatenationB'10001' || B'011'10001011
&bitwise ANDB'10001' & B'01101'00001
|bitwise ORB'10001' | B'01101'11101
#bitwise XORB'10001' # B'01101'11100
~bitwise NOT~ B'10001'01110
<<bitwise shift leftB'10001' << 301000
>>bitwise shift rightB'10001' >> 200100

PostgreSQL bit_count?

需要自定義, 寫兩個C函數來解決

> vi bit_count.c #include "postgres.h" #include "fmgr.h" #include "utils/varbit.h" PG_MODULE_MAGIC;PG_FUNCTION_INFO_V1(bit_count1); PG_FUNCTION_INFO_V1(bit_count2);Datum bit_count1(PG_FUNCTION_ARGS) {VarBit *arg = PG_GETARG_VARBIT_P(0);uint32 mask;bits8 *r;int nbits = 0;/* Check that the bit string is not too long */if (VARBITLEN(arg) > 32)ereport(ERROR,(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),errmsg("integer out of range")));mask = 0;for (r = VARBITS(arg); r < VARBITEND(arg); r++){mask <<= BITS_PER_BYTE;mask |= *r;}/* Now shift the mask to take account of the padding at the end */mask >>= VARBITPAD(arg);/* this code relies on mask being an unsigned type */while (mask){if (mask & 1)nbits++;mask >>= 1;}PG_RETURN_INT32(nbits); }Datum bit_count2(PG_FUNCTION_ARGS) {uint32 mask = PG_GETARG_INT32(0);int nbits = 0;while (mask){if (mask & 1)nbits++;mask >>= 1;}PG_RETURN_INT32(nbits); }> gcc -O3 -Wall -Wextra -Werror -I /home/digoal/postgresql-9.5.0/src/include -g -fPIC -c ./bit_count.c -o bit_count.o > gcc -O3 -Wall -Wextra -Werror -I /home/digoal/postgresql-9.5.0/src/include -g -shared bit_count.o -o libbit_count.so > cp libbit_count.so /home/digoal/pgsql9.5/lib/ > psql postgres=# create or replace function bit_count(varbit) returns int as '$libdir/libbit_count.so', 'bit_count1' language c strict ; CREATE FUNCTION postgres=# create or replace function bit_count(int) returns int as '$libdir/libbit_count.so', 'bit_count2' language c strict ; CREATE FUNCTION postgres=# select bit_count(bit'1111');bit_count -----------4 (1 row) postgres=# select bit_count(bit'1111011001');bit_count -----------7 (1 row)postgres=# select bit_count(99);bit_count -----------4 (1 row)postgres=# select bit_count(10);bit_count -----------2 (1 row)

PostgreSQL還支持set_bit

set_bit postgres=# select set_bit(bit'11111',1,0);set_bit ---------10111 (1 row) postgres=# select set_bit(bit'11111',0,0);set_bit ---------01111 (1 row)

總結

以上是生活随笔為你收集整理的PostgreSQL MySQL 兼容性之 - bit 函数和操作符的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。