]> begriffs open source - ai-pg/blob - full-docs/txt/functions-bitstring.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / functions-bitstring.txt
1
2 9.6. Bit String Functions and Operators #
3
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
9    shown in Table 9.14.
10
11    Table 9.14. Bit String Operators
12
13    Operator
14
15    Description
16
17    Example(s)
18
19    bit || bit → bit
20
21    Concatenation
22
23    B'10001' || B'011' → 10001011
24
25    bit & bit → bit
26
27    Bitwise AND (inputs must be of equal length)
28
29    B'10001' & B'01101' → 00001
30
31    bit | bit → bit
32
33    Bitwise OR (inputs must be of equal length)
34
35    B'10001' | B'01101' → 11101
36
37    bit # bit → bit
38
39    Bitwise exclusive OR (inputs must be of equal length)
40
41    B'10001' # B'01101' → 11100
42
43    ~ bit → bit
44
45    Bitwise NOT
46
47    ~ B'10001' → 01110
48
49    bit << integer → bit
50
51    Bitwise shift left (string length is preserved)
52
53    B'10001' << 3 → 01000
54
55    bit >> integer → bit
56
57    Bitwise shift right (string length is preserved)
58
59    B'10001' >> 2 → 00100
60
61    Some of the functions available for binary strings are also available
62    for bit strings, as shown in Table 9.15.
63
64    Table 9.15. Bit String Functions
65
66    Function
67
68    Description
69
70    Example(s)
71
72    bit_count ( bit ) → bigint
73
74    Returns the number of bits set in the bit string (also known as
75    “popcount”).
76
77    bit_count(B'10111') → 4
78
79    bit_length ( bit ) → integer
80
81    Returns number of bits in the bit string.
82
83    bit_length(B'10111') → 5
84
85    length ( bit ) → integer
86
87    Returns number of bits in the bit string.
88
89    length(B'10111') → 5
90
91    octet_length ( bit ) → integer
92
93    Returns number of bytes in the bit string.
94
95    octet_length(B'1011111011') → 2
96
97    overlay ( bits bit PLACING newsubstring bit FROM start integer [ FOR
98    count integer ] ) → bit
99
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.
103
104    overlay(B'01010101010101010' placing B'11111' from 2 for 3) →
105    0111110101010101010
106
107    position ( substring bit IN bits bit ) → integer
108
109    Returns first starting index of the specified substring within bits, or
110    zero if it's not present.
111
112    position(B'010' in B'000001101011') → 8
113
114    substring ( bits bit [ FROM start integer ] [ FOR count integer ] ) →
115    bit
116
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.
120
121    substring(B'110010111111' from 3 for 2) → 00
122
123    get_bit ( bits bit, n integer ) → integer
124
125    Extracts n'th bit from bit string; the first (leftmost) bit is bit 0.
126
127    get_bit(B'101010101010101010', 6) → 1
128
129    set_bit ( bits bit, n integer, newvalue integer ) → bit
130
131    Sets n'th bit in bit string to newvalue; the first (leftmost) bit is
132    bit 0.
133
134    set_bit(B'101010101010101010', 6, 0) → 101010001010101010
135
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
141 44::bit(3)                     100
142 cast(-44 as bit(12))           111111010100
143 '1110'::bit(4)::integer        14
144
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.