<?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>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><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 11.4 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></hr></div><div class="sect1" id="DATATYPE-BIT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.10. Bit String Types</h2></div></div></div><a id="id-1.5.7.18.2" class="indexterm"></a><p> Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: <code class="type">bit(<em class="replaceable"><code>n</code></em>)</code> and <code class="type">bit varying(<em class="replaceable"><code>n</code></em>)</code>, where <em class="replaceable"><code>n</code></em> is a positive integer. </p><p> <code class="type">bit</code> type data must match the length <em class="replaceable"><code>n</code></em> exactly; it is an error to attempt to store shorter or longer bit strings. <code class="type">bit varying</code> data is of variable length up to the maximum length <em class="replaceable"><code>n</code></em>; longer strings will be rejected. Writing <code class="type">bit</code> without a length is equivalent to <code class="literal">bit(1)</code>, while <code class="type">bit varying</code> without a length specification means unlimited length. </p><div class="note"><h3 class="title">Note</h3><p> If one explicitly casts a bit-string value to <code class="type">bit(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated or zero-padded on the right to be exactly <em class="replaceable"><code>n</code></em> bits, without raising an error. Similarly, if one explicitly casts a bit-string value to <code class="type">bit varying(<em class="replaceable"><code>n</code></em>)</code>, it will be truncated on the right if it is more than <em class="replaceable"><code>n</code></em> bits. </p></div><p> 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 of bit string constants. Bit-logical operators and string manipulation functions are available; see <a class="xref" href="functions-bitstring.html" title="9.6. Bit String Functions and Operators">Section 9.6</a>. </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"> CREATE TABLE test (a BIT(3), b BIT VARYING(5)); INSERT INTO test VALUES (B'101', B'00'); INSERT INTO test VALUES (B'10', B'101'); <code class="computeroutput"> ERROR: bit string length 2 does not match type bit(3) </code> INSERT INTO test VALUES (B'10'::bit(3), B'101'); SELECT * FROM test; <code class="computeroutput"> a | b -----+----- 101 | 00 100 | 101 </code> </pre></div></div><br class="example-break" /><p> A bit string value requires 1 byte for each group of 8 bits, plus 5 or 8 bytes overhead depending on the length of the string (but long values may be compressed or moved out-of-line, as explained in <a class="xref" href="datatype-character.html" title="8.3. Character Types">Section 8.3</a> for character strings). </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-textsearch.html">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">Home</a></td><td width="40%" align="right" valign="top"> 8.11. Text Search Types</td></tr></table></div></body></html>