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 Table 8.1, as well as the operators shown in Table 8.14.
Table 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 8.15.
Table 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:
Note that casting to just “bit” means casting to bit(1)
, and so will deliver only the least significant bit of the integer.