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