CREATE EXTENSION popcount; ERROR: extension "popcount" already exists SELECT popcount32(15::bit(4)); popcount32 ------------ 4 (1 row) SELECT popcount32(7::bit(4)); popcount32 ------------ 3 (1 row) SELECT popcount32(15::bit(8)); popcount32 ------------ 4 (1 row) SELECT popcount32(7::bit(8)); popcount32 ------------ 3 (1 row) SELECT popcount32(15::bit(12)); popcount32 ------------ 4 (1 row) SELECT popcount32(7::bit(12)); popcount32 ------------ 3 (1 row) SELECT popcount32(15::bit(32)); popcount32 ------------ 4 (1 row) SELECT popcount32(7::bit(32)); popcount32 ------------ 3 (1 row) SELECT popcount32(30::bit(32)); popcount32 ------------ 4 (1 row) SELECT popcount32(127::bit(32)); popcount32 ------------ 7 (1 row) SELECT popcount32(b'10101010101010101010101010101010101101010101010101010101010101010101011010101010101010101010101010101010110101010101010101010101010101010101101010101010101010101010101010101011010101010101010101010101010101010110101010101010101010101010101010101101010101010101010101010101010101011010101010101010101010101010101010110101010101010101010101010101010101101010101010101010101010101010101011010101010101010101010101010101010110101010101010101010101010101010101101010101010101010101010101010101011010101010101010101010'); popcount32 ------------ 263 (1 row) SELECT sum(popcount32(x::bit(512))) from generate_series(1,1000000) x; sum --------- 9884999 (1 row) SELECT sum(popcount32(x::bit(500))) from generate_series(1,1000000) x; sum --------- 9884999 (1 row)