]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/datatype-bit.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / datatype-bit.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>8.10. Bit String Types</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="datatype-net-types.html" title="8.9. Network Address Types" /><link rel="next" href="datatype-textsearch.html" title="8.11. Text Search Types" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.10. Bit String Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype-net-types.html" title="8.9. Network Address Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</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="datatype-textsearch.html" title="8.11. Text Search Types">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-BIT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.10. Bit String Types <a href="#DATATYPE-BIT" class="id_link">#</a></h2></div></div></div><a id="id-1.5.7.18.2" class="indexterm"></a><p>
3     Bit strings are strings of 1's and 0's.  They can be used to store
4     or visualize bit masks.  There are two SQL bit types:
5     <code class="type">bit(<em class="replaceable"><code>n</code></em>)</code> and <code class="type">bit
6     varying(<em class="replaceable"><code>n</code></em>)</code>, where
7     <em class="replaceable"><code>n</code></em> is a positive integer.
8    </p><p>
9     <code class="type">bit</code> type data must match the length
10     <em class="replaceable"><code>n</code></em> exactly; it is an error to attempt to
11     store shorter or longer bit strings.  <code class="type">bit varying</code> data is
12     of variable length up to the maximum length
13     <em class="replaceable"><code>n</code></em>; longer strings will be rejected.
14     Writing <code class="type">bit</code> without a length is equivalent to
15     <code class="literal">bit(1)</code>, while <code class="type">bit varying</code> without a length
16     specification means unlimited length.
17    </p><div class="note"><h3 class="title">Note</h3><p>
18      If one explicitly casts a bit-string value to
19      <code class="type">bit(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated or
20      zero-padded on the right to be exactly <em class="replaceable"><code>n</code></em> bits,
21      without raising an error.  Similarly,
22      if one explicitly casts a bit-string value to
23      <code class="type">bit varying(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated
24      on the right if it is more than <em class="replaceable"><code>n</code></em> bits.
25     </p></div><p>
26     Refer to <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-BIT-STRINGS" title="4.1.2.5. Bit-String Constants">Section 4.1.2.5</a> for information about the syntax
27     of bit string constants.  Bit-logical operators and string
28     manipulation functions are available; see <a class="xref" href="functions-bitstring.html" title="9.6. Bit String Functions and Operators">Section 9.6</a>.
29    </p><div class="example" id="id-1.5.7.18.7"><p class="title"><strong>Example 8.3. Using the Bit String Types</strong></p><div class="example-contents"><pre class="programlisting">
30 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
31 INSERT INTO test VALUES (B'101', B'00');
32 INSERT INTO test VALUES (B'10', B'101');
33 <code class="computeroutput">
34 ERROR:  bit string length 2 does not match type bit(3)
35 </code>
36 INSERT INTO test VALUES (B'10'::bit(3), B'101');
37 SELECT * FROM test;
38 <code class="computeroutput">
39   a  |  b
40 -----+-----
41  101 | 00
42  100 | 101
43 </code>
44 </pre></div></div><br class="example-break" /><p>
45     A bit string value requires 1 byte for each group of 8 bits, plus
46     5 or 8 bytes overhead depending on the length of the string
47     (but long values may be compressed or moved out-of-line, as explained
48     in <a class="xref" href="datatype-character.html" title="8.3. Character Types">Section 8.3</a> for character strings).
49    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype-net-types.html" title="8.9. Network Address Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-textsearch.html" title="8.11. Text Search Types">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.9. Network Address Types </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"> 8.11. Text Search Types</td></tr></table></div></body></html>