2 9.6. Bit String Functions and Operators #
4 This section describes functions and operators for examining and
5 manipulating bit strings, that is values of the types bit and bit
6 varying. (While only type bit is mentioned in these tables, values of
7 type bit varying can be used interchangeably.) Bit strings support the
8 usual comparison operators shown in Table 9.1, as well as the operators
11 Table 9.14. Bit String Operators
23 B'10001' || B'011' → 10001011
27 Bitwise AND (inputs must be of equal length)
29 B'10001' & B'01101' → 00001
33 Bitwise OR (inputs must be of equal length)
35 B'10001' | B'01101' → 11101
39 Bitwise exclusive OR (inputs must be of equal length)
41 B'10001' # B'01101' → 11100
51 Bitwise shift left (string length is preserved)
57 Bitwise shift right (string length is preserved)
61 Some of the functions available for binary strings are also available
62 for bit strings, as shown in Table 9.15.
64 Table 9.15. Bit String Functions
72 bit_count ( bit ) → bigint
74 Returns the number of bits set in the bit string (also known as
77 bit_count(B'10111') → 4
79 bit_length ( bit ) → integer
81 Returns number of bits in the bit string.
83 bit_length(B'10111') → 5
85 length ( bit ) → integer
87 Returns number of bits in the bit string.
91 octet_length ( bit ) → integer
93 Returns number of bytes in the bit string.
95 octet_length(B'1011111011') → 2
97 overlay ( bits bit PLACING newsubstring bit FROM start integer [ FOR
98 count integer ] ) → bit
100 Replaces the substring of bits that starts at the start'th bit and
101 extends for count bits with newsubstring. If count is omitted, it
102 defaults to the length of newsubstring.
104 overlay(B'01010101010101010' placing B'11111' from 2 for 3) →
107 position ( substring bit IN bits bit ) → integer
109 Returns first starting index of the specified substring within bits, or
110 zero if it's not present.
112 position(B'010' in B'000001101011') → 8
114 substring ( bits bit [ FROM start integer ] [ FOR count integer ] ) →
117 Extracts the substring of bits starting at the start'th bit if that is
118 specified, and stopping after count bits if that is specified. Provide
119 at least one of start and count.
121 substring(B'110010111111' from 3 for 2) → 00
123 get_bit ( bits bit, n integer ) → integer
125 Extracts n'th bit from bit string; the first (leftmost) bit is bit 0.
127 get_bit(B'101010101010101010', 6) → 1
129 set_bit ( bits bit, n integer, newvalue integer ) → bit
131 Sets n'th bit in bit string to newvalue; the first (leftmost) bit is
134 set_bit(B'101010101010101010', 6, 0) → 101010001010101010
136 In addition, it is possible to cast integral values to and from type
137 bit. Casting an integer to bit(n) copies the rightmost n bits. Casting
138 an integer to a bit string width wider than the integer itself will
139 sign-extend on the left. Some examples:
140 44::bit(10) 0000101100
142 cast(-44 as bit(12)) 111111010100
143 '1110'::bit(4)::integer 14
145 Note that casting to just “bit” means casting to bit(1), and so will
146 deliver only the least significant bit of the integer.