1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.6. Bit String Functions and Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="functions-binarystring.html" title="9.5. Binary String Functions and Operators" /><link rel="next" href="functions-matching.html" title="9.7. Pattern Matching" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.6. Bit String Functions and Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-binarystring.html" title="9.5. Binary String Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-matching.html" title="9.7. Pattern Matching">Next</a></td></tr></table><hr /></div><div class="sect1" id="FUNCTIONS-BITSTRING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.6. Bit String Functions and Operators</h2></div></div></div><a id="id-1.5.8.12.2" class="indexterm"></a><p>
This section describes functions and operators for examining and
manipulating bit strings, that is values of the types
<code class="type">bit</code> and <code class="type">bit varying</code>. (While only
type <code class="type">bit</code> is mentioned in these tables, values of
type <code class="type">bit varying</code> can be used interchangeably.)
Bit strings support the usual comparison operators shown in
<a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE" title="Table 9.1. Comparison Operators">Table 9.1</a>, as well as the
operators shown in <a class="xref" href="functions-bitstring.html#FUNCTIONS-BIT-STRING-OP-TABLE" title="Table 9.14. Bit String Operators">Table 9.14</a>.
</p><div class="table" id="FUNCTIONS-BIT-STRING-OP-TABLE"><p class="title"><strong>Table 9.14. Bit String Operators</strong></p><div class="table-contents"><table class="table" summary="Bit String Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
Operator
</p>
<p>
Description
</p>
<p>
Example(s)
</p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal">||</code> <code class="type">bit</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Concatenation
</p>
<p>
<code class="literal">B'10001' || B'011'</code>
→ <code class="returnvalue">10001011</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal">&</code> <code class="type">bit</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise AND (inputs must be of equal length)
</p>
<p>
<code class="literal">B'10001' & B'01101'</code>
→ <code class="returnvalue">00001</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal">|</code> <code class="type">bit</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise OR (inputs must be of equal length)
</p>
<p>
<code class="literal">B'10001' | B'01101'</code>
→ <code class="returnvalue">11101</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal">#</code> <code class="type">bit</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise exclusive OR (inputs must be of equal length)
</p>
<p>
<code class="literal">B'10001' # B'01101'</code>
→ <code class="returnvalue">11100</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="literal">~</code> <code class="type">bit</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise NOT
</p>
<p>
<code class="literal">~ B'10001'</code>
→ <code class="returnvalue">01110</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal"><<</code> <code class="type">integer</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise shift left
(string length is preserved)
</p>
<p>
<code class="literal">B'10001' << 3</code>
→ <code class="returnvalue">01000</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<code class="type">bit</code> <code class="literal">>></code> <code class="type">integer</code>
→ <code class="returnvalue">bit</code>
</p>
<p>
Bitwise shift right
(string length is preserved)
</p>
<p>
<code class="literal">B'10001' >> 2</code>
→ <code class="returnvalue">00100</code>
</p></td></tr></tbody></table></div></div><br class="table-break" /><p>
Some of the functions available for binary strings are also available
for bit strings, as shown in <a class="xref" href="functions-bitstring.html#FUNCTIONS-BIT-STRING-TABLE" title="Table 9.15. Bit String Functions">Table 9.15</a>.
</p><div class="table" id="FUNCTIONS-BIT-STRING-TABLE"><p class="title"><strong>Table 9.15. Bit String Functions</strong></p><div class="table-contents"><table class="table" summary="Bit String Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
Function
</p>
<p>
Description
</p>
<p>
Example(s)
</p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.1.1.1.1" class="indexterm"></a>
<code class="function">bit_count</code> ( <code class="type">bit</code> )
→ <code class="returnvalue">bigint</code>
</p>
<p>
Returns the number of bits set in the bit string (also known as
<span class="quote">“<span class="quote">popcount</span>”</span>).
</p>
<p>
<code class="literal">bit_count(B'10111')</code>
→ <code class="returnvalue">4</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.2.1.1.1" class="indexterm"></a>
<code class="function">bit_length</code> ( <code class="type">bit</code> )
→ <code class="returnvalue">integer</code>
</p>
<p>
Returns number of bits in the bit string.
</p>
<p>
<code class="literal">bit_length(B'10111')</code>
→ <code class="returnvalue">5</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.3.1.1.1" class="indexterm"></a>
<a id="id-1.5.8.12.6.2.2.3.1.1.2" class="indexterm"></a>
<code class="function">length</code> ( <code class="type">bit</code> )
→ <code class="returnvalue">integer</code>
</p>
<p>
Returns number of bits in the bit string.
</p>
<p>
<code class="literal">length(B'10111')</code>
→ <code class="returnvalue">5</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.4.1.1.1" class="indexterm"></a>
<code class="function">octet_length</code> ( <code class="type">bit</code> )
→ <code class="returnvalue">integer</code>
</p>
<p>
Returns number of bytes in the bit string.
</p>
<p>
<code class="literal">octet_length(B'1011111011')</code>
→ <code class="returnvalue">2</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.5.1.1.1" class="indexterm"></a>
<code class="function">overlay</code> ( <em class="parameter"><code>bits</code></em> <code class="type">bit</code> <code class="literal">PLACING</code> <em class="parameter"><code>newsubstring</code></em> <code class="type">bit</code> <code class="literal">FROM</code> <em class="parameter"><code>start</code></em> <code class="type">integer</code> [<span class="optional"> <code class="literal">FOR</code> <em class="parameter"><code>count</code></em> <code class="type">integer</code> </span>] )
→ <code class="returnvalue">bit</code>
</p>
<p>
Replaces the substring of <em class="parameter"><code>bits</code></em> that starts at
the <em class="parameter"><code>start</code></em>'th bit and extends
for <em class="parameter"><code>count</code></em> bits
with <em class="parameter"><code>newsubstring</code></em>.
If <em class="parameter"><code>count</code></em> is omitted, it defaults to the length
of <em class="parameter"><code>newsubstring</code></em>.
</p>
<p>
<code class="literal">overlay(B'01010101010101010' placing B'11111' from 2 for 3)</code>
→ <code class="returnvalue">0111110101010101010</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.6.1.1.1" class="indexterm"></a>
<code class="function">position</code> ( <em class="parameter"><code>substring</code></em> <code class="type">bit</code> <code class="literal">IN</code> <em class="parameter"><code>bits</code></em> <code class="type">bit</code> )
→ <code class="returnvalue">integer</code>
</p>
<p>
Returns first starting index of the specified <em class="parameter"><code>substring</code></em>
within <em class="parameter"><code>bits</code></em>, or zero if it's not present.
</p>
<p>
<code class="literal">position(B'010' in B'000001101011')</code>
→ <code class="returnvalue">8</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.7.1.1.1" class="indexterm"></a>
<code class="function">substring</code> ( <em class="parameter"><code>bits</code></em> <code class="type">bit</code> [<span class="optional"> <code class="literal">FROM</code> <em class="parameter"><code>start</code></em> <code class="type">integer</code> </span>] [<span class="optional"> <code class="literal">FOR</code> <em class="parameter"><code>count</code></em> <code class="type">integer</code> </span>] )
→ <code class="returnvalue">bit</code>
</p>
<p>
Extracts the substring of <em class="parameter"><code>bits</code></em> starting at
the <em class="parameter"><code>start</code></em>'th bit if that is specified,
and stopping after <em class="parameter"><code>count</code></em> bits if that is
specified. Provide at least one of <em class="parameter"><code>start</code></em>
and <em class="parameter"><code>count</code></em>.
</p>
<p>
<code class="literal">substring(B'110010111111' from 3 for 2)</code>
→ <code class="returnvalue">00</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.8.1.1.1" class="indexterm"></a>
<code class="function">get_bit</code> ( <em class="parameter"><code>bits</code></em> <code class="type">bit</code>,
<em class="parameter"><code>n</code></em> <code class="type">integer</code> )
→ <code class="returnvalue">integer</code>
</p>
<p>
Extracts <em class="parameter"><code>n</code></em>'th bit
from bit string; the first (leftmost) bit is bit 0.
</p>
<p>
<code class="literal">get_bit(B'101010101010101010', 6)</code>
→ <code class="returnvalue">1</code>
</p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
<a id="id-1.5.8.12.6.2.2.9.1.1.1" class="indexterm"></a>
<code class="function">set_bit</code> ( <em class="parameter"><code>bits</code></em> <code class="type">bit</code>,
<em class="parameter"><code>n</code></em> <code class="type">integer</code>,
<em class="parameter"><code>newvalue</code></em> <code class="type">integer</code> )
→ <code class="returnvalue">bit</code>
</p>
<p>
Sets <em class="parameter"><code>n</code></em>'th bit in
bit string to <em class="parameter"><code>newvalue</code></em>;
the first (leftmost) bit is bit 0.
</p>
<p>
<code class="literal">set_bit(B'101010101010101010', 6, 0)</code>
→ <code class="returnvalue">101010001010101010</code>
</p></td></tr></tbody></table></div></div><br class="table-break" /><p>
In addition, it is possible to cast integral values to and from type
<code class="type">bit</code>.
Casting an integer to <code class="type">bit(n)</code> copies the rightmost
<code class="literal">n</code> bits. Casting an integer to a bit string width wider
than the integer itself will sign-extend on the left.
Some examples:
</p><pre class="programlisting">
44::bit(10) <em class="lineannotation"><span class="lineannotation">0000101100</span></em>
44::bit(3) <em class="lineannotation"><span class="lineannotation">100</span></em>
cast(-44 as bit(12)) <em class="lineannotation"><span class="lineannotation">111111010100</span></em>
'1110'::bit(4)::integer <em class="lineannotation"><span class="lineannotation">14</span></em>
</pre><p>
Note that casting to just <span class="quote">“<span class="quote">bit</span>”</span> means casting to
<code class="literal">bit(1)</code>, and so will deliver only the least significant
bit of the integer.
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-binarystring.html" title="9.5. Binary String Functions and Operators">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-matching.html" title="9.7. Pattern Matching">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.5. Binary String Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.7. Pattern Matching</td></tr></table></div></body></html>
|