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/
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
-----------
(1 row)
postgres=# select bit_count(bit'1111011001');
bit_count
-----------
(1 row)
postgres=# select bit_count(99);
bit_count
-----------
(1 row)
postgres=# select bit_count(10);
bit_count
-----------
(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)
PolarDB 开源版 使用PostGIS 以及泰森多边形 解决 "零售、配送、综合体、教培、连锁店等经营"|"通信行业基站建设功率和指向" 的地理最优解问题
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 2
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 2
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 1
【MySQL速通篇003】MySQL视图,MySQL触发器,MySQL函数,MySQL存储过程(参数分类,存储过程的增删改查等),SQL的动态执行,支持事务的存储过程,pymysql 1