Bitwise functions#
-
bit_count
(x, bits) → bigint# Count the number of bits set in
x
(treated asbits
-bit signed integer) 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
-
bitwise_and
(x, y) → bigint# Returns the bitwise AND of
x
andy
in 2’s complement representation.
-
bitwise_not
(x) → bigint# Returns the bitwise NOT of
x
in 2’s complement representation.
-
bitwise_or
(x, y) → bigint# Returns the bitwise OR of
x
andy
in 2’s complement representation.
-
bitwise_xor
(x, y) → bigint# Returns the bitwise XOR of
x
andy
in 2’s complement representation.
-
bitwise_left_shift
(value, shift) → [same as value]# Returns the left shifted value of
value
.Shifting
1
(binary:001
) by two bits results in4
(binary:00100
):SELECT bitwise_left_shift(1, 2); -- 4
Shifting
5
(binary:0101
) by two bits results in20
(binary:010100
):SELECT bitwise_left_shift(5, 2); -- 20
Shifting a
value
by0
always results in the originalvalue
:SELECT bitwise_left_shift(20 , 0); -- 20 SELECT bitwise_left_shift(42 , 0); -- 42
Shifting
0
by avalue
always results in0
:SELECT bitwise_left_shift(0, 1); -- 0 SELECT bitwise_left_shift(0, 2); -- 0
-
bitwise_right_shift
(value, shift, digits) → [same as value]# Returns the logical right shifted value of
value
.
-
bitwise_right_shift_arithmetic
(value, shift) → [same as value]# Returns the arithmetic right shifted value of
value
.
See also bitwise_and_agg()
and bitwise_or_agg()
.