9.6. Bit String Functions and Operators
This section describes functions and operators for examining and manipulating bit strings, that is values of the types bit
and bit varying
. (While only type bit
is mentioned in these tables, values of type bit varying
can be used interchangeably.) Bit strings support the usual comparison operators shown in Table 9.1, as well as the operators shown in Table 9.14.
Table 9.14. Bit String Operators
Operator Description Example(s) |
---|
Concatenation
|
Bitwise AND (inputs must be of equal length)
|
Bitwise OR (inputs must be of equal length)
|
Bitwise exclusive OR (inputs must be of equal length)
|
Bitwise NOT
|
Bitwise shift left (string length is preserved)
|
Bitwise shift right (string length is preserved)
|
Some of the functions available for binary strings are also available for bit strings, as shown in Table 9.15.
Table 9.15. Bit String Functions
Function Description Example(s) |
---|
Returns the number of bits set in the bit string (also known as “popcount”).
|
Returns number of bits in the bit string.
|
Returns number of bits in the bit string.
|
Returns number of bytes in the bit string.
|
Replaces the substring of
|
Returns first starting index of the specified
|
Extracts the substring of
|
Extracts
|
Sets
|
In addition, it is possible to cast integral values to and from type bit
. Casting an integer to bit(n)
copies the rightmost n
bits. Casting an integer to a bit string width wider than the integer itself will sign-extend on the left. Some examples:
44::bit(10) 0000101100 44::bit(3) 100 cast(-44 as bit(12)) 111111010100 '1110'::bit(4)::integer 14
Note that casting to just “bit” means casting to bit(1)
, and so will deliver only the least significant bit of the integer.
© 1996–2021 The PostgreSQL Global Development Group
Licensed under the PostgreSQL License.
https://www.postgresql.org/docs/14/functions-bitstring.html