9.6. Bit String Functions and Operators
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 Comparison Operators, as well as the operators shown in 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)
|
: Bit String Operators
Some of the functions available for binary strings are also available for bit strings, as shown in 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
|
: Bit String Functions
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.