11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.208 2004/06/14 19: 01:09 tgl Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.209 2004/06/16 01:26:36 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -488,55 +488,13 @@ PostgreSQL documentation
488488 </table>
489489
490490 <para>
491- The bitwise operators are also available for the bit
491+ The bitwise operators work only on integral data types, whereas
492+ the others are available for all numeric data types. The bitwise
493+ operators are also available for the bit
492494 string types <type>bit</type> and <type>bit varying</type>, as
493- shown in <xref linkend="functions-math-bit-table">.
494- Bit string operands of <literal>&</literal>, <literal>|</literal>,
495- and <literal>#</literal> must be of equal length. When bit
496- shifting, the original length of the string is preserved, as shown
497- in the table.
495+ shown in <xref linkend="functions-bit-string-op-table">.
498496 </para>
499497
500- <table id="functions-math-bit-table">
501- <title>Bit String Bitwise Operators</title>
502-
503- <tgroup cols="2">
504- <thead>
505- <row>
506- <entry>Example</entry>
507- <entry>Result</entry>
508- </row>
509- </thead>
510-
511- <tbody>
512- <row>
513- <entry><literal>B'10001' & B'01101'</literal></entry>
514- <entry><literal>00001</literal></entry>
515- </row>
516- <row>
517- <entry><literal>B'10001' | B'01101'</literal></entry>
518- <entry><literal>11101</literal></entry>
519- </row>
520- <row>
521- <entry><literal>B'10001' # B'01101'</literal></entry>
522- <entry><literal>11110</literal></entry>
523- </row>
524- <row>
525- <entry><literal>~ B'10001'</literal></entry>
526- <entry><literal>01110</literal></entry>
527- </row>
528- <row>
529- <entry><literal>B'10001' << 3</literal></entry>
530- <entry><literal>01000</literal></entry>
531- </row>
532- <row>
533- <entry><literal>B'10001' >> 2</literal></entry>
534- <entry><literal>00100</literal></entry>
535- </row>
536- </tbody>
537- </tgroup>
538- </table>
539-
540498 <para>
541499 <xref linkend="functions-math-func-table"> shows the available
542500 mathematical functions. In the table, <literal>dp</literal>
@@ -2337,6 +2295,130 @@ PostgreSQL documentation
23372295 </sect1>
23382296
23392297
2298+ <sect1 id="functions-bitstring">
2299+ <title>Bit String Functions and Operators</title>
2300+
2301+ <indexterm zone="functions-bitstring">
2302+ <primary>bit strings</primary>
2303+ <secondary>functions</secondary>
2304+ </indexterm>
2305+
2306+ <para>
2307+ This section describes functions and operators for examining and
2308+ manipulating bit strings, that is values of the types
2309+ <type>bit</type> and <type>bit varying</type>. Aside from the
2310+ usual comparison operators, the operators
2311+ shown in <xref linkend="functions-bit-string-op-table"> can be used.
2312+ Bit string operands of <literal>&</literal>, <literal>|</literal>,
2313+ and <literal>#</literal> must be of equal length. When bit
2314+ shifting, the original length of the string is preserved, as shown
2315+ in the examples.
2316+ </para>
2317+
2318+ <table id="functions-bit-string-op-table">
2319+ <title>Bit String Operators</title>
2320+
2321+ <tgroup cols="4">
2322+ <thead>
2323+ <row>
2324+ <entry>Operator</entry>
2325+ <entry>Description</entry>
2326+ <entry>Example</entry>
2327+ <entry>Result</entry>
2328+ </row>
2329+ </thead>
2330+
2331+ <tbody>
2332+ <row>
2333+ <entry> <literal>||</literal> </entry>
2334+ <entry>concatenation</entry>
2335+ <entry><literal>B'10001' || B'011'</literal></entry>
2336+ <entry><literal>10001011</literal></entry>
2337+ </row>
2338+
2339+ <row>
2340+ <entry> <literal>&</literal> </entry>
2341+ <entry>bitwise AND</entry>
2342+ <entry><literal>B'10001' & B'01101'</literal></entry>
2343+ <entry><literal>00001</literal></entry>
2344+ </row>
2345+
2346+ <row>
2347+ <entry> <literal>|</literal> </entry>
2348+ <entry>bitwise OR</entry>
2349+ <entry><literal>B'10001' | B'01101'</literal></entry>
2350+ <entry><literal>11101</literal></entry>
2351+ </row>
2352+
2353+ <row>
2354+ <entry> <literal>#</literal> </entry>
2355+ <entry>bitwise XOR</entry>
2356+ <entry><literal>B'10001' # B'01101'</literal></entry>
2357+ <entry><literal>11100</literal></entry>
2358+ </row>
2359+
2360+ <row>
2361+ <entry> <literal>~</literal> </entry>
2362+ <entry>bitwise NOT</entry>
2363+ <entry><literal>~ B'10001'</literal></entry>
2364+ <entry><literal>01110</literal></entry>
2365+ </row>
2366+
2367+ <row>
2368+ <entry> <literal><<</literal> </entry>
2369+ <entry>bitwise shift left</entry>
2370+ <entry><literal>B'10001' << 3</literal></entry>
2371+ <entry><literal>01000</literal></entry>
2372+ </row>
2373+
2374+ <row>
2375+ <entry> <literal>>></literal> </entry>
2376+ <entry>bitwise shift right</entry>
2377+ <entry><literal>B'10001' >> 2</literal></entry>
2378+ <entry><literal>00100</literal></entry>
2379+ </row>
2380+ </tbody>
2381+ </tgroup>
2382+ </table>
2383+
2384+ <para>
2385+ The following <acronym>SQL</acronym>-standard functions work on bit
2386+ strings as well as character strings:
2387+ <literal><function>length</function></literal>,
2388+ <literal><function>bit_length</function></literal>,
2389+ <literal><function>octet_length</function></literal>,
2390+ <literal><function>position</function></literal>,
2391+ <literal><function>substring</function></literal>.
2392+ </para>
2393+
2394+ <para>
2395+ In addition, it is possible to cast integral values to and from type
2396+ <type>bit</>.
2397+ Some examples:
2398+ <programlisting>
2399+ 44::bit(10) <lineannotation>0000101100</lineannotation>
2400+ 44::bit(3) <lineannotation>100</lineannotation>
2401+ cast(-44 as bit(12)) <lineannotation>111111010100</lineannotation>
2402+ '1110'::bit(4)::integer <lineannotation>14</lineannotation>
2403+ </programlisting>
2404+ Note that casting to just <quote>bit</> means casting to
2405+ <literal>bit(1)</>, and so it will deliver only the least significant
2406+ bit of the integer.
2407+ </para>
2408+
2409+ <note>
2410+ <para>
2411+ Prior to <productname>PostgreSQL</productname> 7.5, casting an
2412+ integer to <type>bit(n)</> would copy the leftmost <literal>n</>
2413+ bits of the integer, whereas now it copies the rightmost <literal>n</>
2414+ bits. Also, casting an integer to a bit string width wider than
2415+ the integer itself will sign-extend on the left.
2416+ </para>
2417+ </note>
2418+
2419+ </sect1>
2420+
2421+
23402422 <sect1 id="functions-matching">
23412423 <title>Pattern Matching</title>
23422424
@@ -7628,14 +7710,13 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
76287710 <function>bit_and(<replaceable class="parameter">expression</replaceable>)</function>
76297711 </entry>
76307712 <entry>
7631- <type>smallint</type>, <type>integer</type>, <type>bigint</type> or
7632- <type>bit</type>,
7713+ <type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
7714+ <type>bit</type>
76337715 </entry>
76347716 <entry>
7635- same as argument data type.
7636- </entry>
7637- <entry>the bitwise-and of all non-null input values, or null if empty
7717+ same as argument data type
76387718 </entry>
7719+ <entry>the bitwise AND of all non-null input values, or null if none</entry>
76397720 </row>
76407721
76417722 <row>
@@ -7646,14 +7727,13 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
76467727 <function>bit_or(<replaceable class="parameter">expression</replaceable>)</function>
76477728 </entry>
76487729 <entry>
7649- <type>smallint</type>, <type>integer</type>, <type>bigint</type> or
7650- <type>bit</type>,
7730+ <type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
7731+ <type>bit</type>
76517732 </entry>
76527733 <entry>
7653- same as argument data type.
7654- </entry>
7655- <entry>the bitwise-or of all non-null input values, or null if empty.
7734+ same as argument data type
76567735 </entry>
7736+ <entry>the bitwise OR of all non-null input values, or null if none</entry>
76577737 </row>
76587738
76597739 <row>
@@ -7669,9 +7749,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
76697749 <entry>
76707750 <type>bool</type>
76717751 </entry>
7672- <entry>true if all input values are true, otherwise false.
7673- Also known as <function>bool_and</function>.
7674- </entry>
7752+ <entry>true if all input values are true, otherwise false</entry>
76757753 </row>
76767754
76777755 <row>
@@ -7720,9 +7798,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
77207798 <entry>
77217799 <type>bool</type>
77227800 </entry>
7723- <entry>true if all input values are true, otherwise false.
7724- Also known as <function>bool_and</function>.
7725- </entry>
7801+ <entry>equivalent to <function>bool_and</function></entry>
77267802 </row>
77277803
77287804 <row>