6.7. Bitwise Functions
bitcount
(_x, bits) → bigint- Count the number of bits set in
x
(treated asbits
-bit signedinteger) in 2’s complement representation:
- SELECT bit_count(9, 64); -- 2
- SELECT bit_count(9, 8); -- 2
- SELECT bit_count(-7, 64); -- 62
- SELECT bit_count(-7, 8); -- 6
bitwiseand
(_x, y) → bigintReturns the bitwise AND of
x
andy
in 2’s complement representation.bitwisenot
(_x) → bigintReturns the bitwise NOT of
x
in 2’s complement representation.bitwiseor
(_x, y) → bigintReturns the bitwise OR of
x
andy
in 2’s complement representation.bitwisexor
(_x, y) → bigintReturns the bitwise XOR of
x
andy
in 2’s complement representation.bitwiseshift_left
(_x, shift, bits) → bigint- Left shift operation on
x
(treated asbits
-bit integer)shifted byshift
.
SELECT bitwise_shift_left(7, 2, 4); – 12SELECT bitwise_shift_left(7, 2, 64); – 28
bitwiselogical_shift_right
(_x, shift, bits) → bigint- Logical right shift operation on
x
(treated asbits
-bit integer)shifted byshift
.
SELECT bitwise_logical_shift_right(7, 2, 4); – 1SELECT bitwise_logical_shift_right(-8, 2, 5); – 6
bitwisearithmetic_shift_right
(_x, shift) → bigint- Arithmetic right shift operation on
x
shifted byshift
in 2’s complement representation.
SELECT bitwise_arithmetic_shift_right(-8, 2); – -2SELECT bitwise_arithmetic_shift_right(7, 2); – 1
See also bitwise_and_agg()
and bitwise_or_agg()
.