PostgreSQL
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) |
---|
|
` `+bit` → Concatenation `+B'10001' |
B'011'` → `+10001011` |
Bitwise AND (inputs must be of equal length)
|
|
` `+bit` → Bitwise OR (inputs must be of equal length) `+B'10001' |
B'01101'` → `+11101` |
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) |
---|
`+bit_count+` ( `+bit+` ) → `+bigint+` Returns the number of bits set in the bit string (also known as “[.quote]#popcount”#).
|
`+bit_length+` ( `+bit+` ) → `+integer+` Returns number of bits in the bit string.
|
`+length+` ( `+bit+` ) → `+integer+` Returns number of bits in the bit string.
|
`+octet_length+` ( `+bit+` ) → `+integer+` Returns number of bytes in the bit string.
|
`+overlay+` ( _`+bits+`_ `+bit+` `+PLACING+` _`+newsubstring+`_ `+bit+` `+FROM+` _`+start+`_ `+integer+` [ [.optional]#`+FOR+` _`+count+`_ `+integer+`# ] ) → `+bit+` Replaces the substring of `bits
|
`+position+` ( _`+substring+`_ `+bit+` `+IN+` _`+bits+`_ `+bit+` ) → `+integer+` Returns first starting index of the specified `substring
|
`+substring+` ( _`+bits+`_ `+bit+` [ [.optional]#`+FROM+` _`+start+`_ `+integer+`# ] [ [.optional]#`+FOR+` _`+count+`_ `+integer+`# ] ) → `+bit+` Extracts the substring of `bits
|
`+get_bit+` ( _`+bits+`_ `+bit+`, _`+n+`_ `+integer+` ) → `+integer+` Extracts `n`'th bit from bit string; the first (leftmost) bit is bit 0.
|
`+set_bit+` ( _`+bits+`_ `+bit+`, _`+n+`_ `+integer+`, _`+newvalue+`_ `+integer+` ) → `+bit+` Sets `n
|
+
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 “[.quote]#bit”# means casting to bit(1)
, and so will deliver only the least significant bit of the integer.
Prev | Up | Next |
---|---|---|
9.5. Binary String Functions and Operators |
9.7. Pattern Matching |
Submit correction
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.
Copyright © 1996-2024 The PostgreSQL Global Development Group