Skip to content

Bit functions ​

Bit operations treat values as bit patterns. Integer operators act on numeric values; byte-string functions act on the bytes of text or BLOB input.

Integer operators ​

text
value & mask
value | mask
~value
value << positions
value >> positions

& is bitwise AND, | is bitwise OR and ~ inverts bits. Shifts move the pattern left or right. Native wide integer operations retain their full width rather than converting the value through floating point.

sql
SELECT 6 & 3, 6 | 3, 1 << 4
FORMAT csv;
text
2,7,16

Binary-string functions ​

get_bit(value, offset) and set_bit(value, offset, bit) read or replace a bit. get_byte and set_byte address bytes. bit_count counts set bits, including bytes after an embedded zero.

sql
SELECT bit_count(X'0F'), bit_count(X'00FF')
FORMAT csv;
text
4,8

See the encoding and binary reference for offsets, accepted values and out-of-range errors. To fold integer bit patterns across rows, use bit_and, bit_or or bit_xor.