Documentation
/ SQL
/ Functions
Bitstring Functions
This section describes functions and operators for examining and manipulating BITSTRING
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.
The table below shows the available mathematical operators for BIT
type.
Operator |
Description |
Example |
Result |
& |
Bitwise AND |
'10101'::BITSTRING & '10001'::BITSTRING |
10001 |
| |
Bitwise OR |
'1011'::BITSTRING | '0001'::BITSTRING |
1011 |
xor |
Bitwise XOR |
xor('101'::BITSTRING, '001'::BITSTRING) |
100 |
~ |
Bitwise NOT |
~('101'::BITSTRING) |
010 |
<< |
Bitwise shift left |
'1001011'::BITSTRING << 3 |
1011000 |
>> |
Bitwise shift right |
'1001011'::BITSTRING >> 3 |
0001001 |
The table below shows the available scalar functions for BIT
type.
Description |
Returns the number of set bits in the bitstring. |
Example |
bit_count('1101011'::BITSTRING) |
Result |
5 |
Description |
Returns the number of bits in the bitstring. |
Example |
bit_length('1101011'::BITSTRING) |
Result |
7 |
Description |
Returns first starting index of the specified substring within bits, or zero if it's not present. The first (leftmost) bit is indexed 1 |
Example |
bit_position('010'::BITSTRING, '1110101'::BITSTRING) |
Result |
4 |
Description |
Returns a bitstring of determined length. |
Example |
bitstring('1010'::BITSTRING, 7) |
Result |
0001010 |
Description |
Extracts the nth bit from bitstring; the first (leftmost) bit is indexed 0. |
Example |
get_bit('0110010'::BITSTRING, 2) |
Result |
1 |
Description |
Alias for bit_length . |
Example |
length('1101011'::BITSTRING) |
Result |
7 |
Description |
Returns the number of bytes in the bitstring. |
Example |
octet_length('1101011'::BITSTRING) |
Result |
1 |
Description |
Sets the nth bit in bitstring to newvalue; the first (leftmost) bit is indexed 0. Returns a new bitstring. |
Example |
set_bit('0110010'::BITSTRING, 2, 0) |
Result |
0100010 |
These aggregate functions are available for BIT
type.
Name |
Description |
bit_and(arg) |
Returns the bitwise AND operation performed on all bitstrings in a given expression. |
bit_or(arg) |
Returns the bitwise OR operation performed on all bitstrings in a given expression. |
bit_xor(arg) |
Returns the bitwise XOR operation performed on all bitstrings in a given expression. |
bitstring_agg(arg) |
Returns a bitstring with bits set for each distinct position defined in arg . |
bitstring_agg(arg, min, max) |
Returns a bitstring with bits set for each distinct position defined in arg . All positions must be within the range [min , max ] or an Out of Range Error will be thrown. |
Description |
Returns the bitwise AND operation performed on all bitstrings in a given expression. |
Example |
bit_and(A) |
Description |
Returns the bitwise OR operation performed on all bitstrings in a given expression. |
Example |
bit_or(A) |
Description |
Returns the bitwise XOR operation performed on all bitstrings in a given expression. |
Example |
bit_xor(A) |
Description |
The bitstring_agg function takes any integer type as input and returns a bitstring with bits set for each distinct value. The left-most bit represents the smallest value in the column and the right-most bit the maximum value. If possible, the min and max are retrieved from the column statistics. Otherwise, it is also possible to provide the min and max values. |
Example |
bitstring_agg(A) |
Tip The combination of bit_count
and bitstring_agg
can be used as an alternative to count(DISTINCT ...)
, with possible performance improvements in cases of low cardinality and dense values.
Description |
Returns a bitstring with bits set for each distinct position defined in arg . All positions must be within the range [min , max ] or an Out of Range Error will be thrown. |
Example |
bitstring_agg(A, 1, 42) |