Bitstring Functions
Version 0.7.1

This section describes functions and operators for examining and manipulating bit values. Bitstrings must be of equal length when performing the bitwise operands AND, OR and XOR. When bit shifting, the original length of the string is preserved.

Bitstring Operators

The table below shows the available mathematical operators for BIT type.

Operator Description Example Result
& bitwise AND '10101'::BIT & '10001'::BIT 10001
| bitwise OR '1011'::BIT | '0001'::BIT 1011
xor bitwise XOR xor('101'::BIT, '001'::BIT) 100
~ bitwise NOT ~('101'::BIT) 010
<< bitwise shift left '1001011'::BIT << 3 1011000
>> bitwise shift right '1001011'::BIT >> 3 0001001

Bitstring Functions

The table below shows the available scalar functions for BIT type.

Function Description Example Result
bit_length(bitstring) Returns the number of bits in the bitstring. bit_length('1101011'::BIT) 7
length(bitstring) Alias for bit_length. length('1101011'::BIT) 7
octet_length(bitstring) Returns the number of bytes in the bitstring. octet_length('1101011'::BIT) 1
bit_count(bitstring) Returns the number of set bits in the bitstring. bit_count('1101011'::BIT) 5
get_bit(bitstring, index) Extracts the nth bit from bitstring; the first (leftmost) bit is indexed 0. get_bit('0110010'::BIT, 2) 1
set_bit(bitstring, index, new_value) Sets the nth bit in bitstring to newvalue; the first (leftmost) bit is indexed 0. Returns a new bitstring. set_bit('0110010'::BIT, 2, 0) 0100010
bit_position(substring, bitstring) Returns first starting index of the specified substring within bits, or zero if it’s not present. The first (leftmost) bit is indexed 1 bit_position('010'::BIT, '1110101'::BIT) 4

Bitstring Aggregate Functions

These aggregate functions are available for BIT type.

Function Description Example
bit_and(arg) Returns the bitwise AND operation performed on all bitstrings in a given expression. bit_and(A)
bit_or(arg) Returns the bitwise OR operation performed on all bitstrings in a given expression. bit_or(A)
bit_xor(arg) Returns the bitwise XOR operation performed on all bitstrings in a given expression. bit_xor(A)
Search Shortcut cmd + k | ctrl + k