Text Size: Normal / Large

9.6. Bit String Functions and Operators

This section describes functions and operators for examining and manipulating bit strings, that is values of the types bit and bit varying. Aside from the usual comparison operators, the operators shown in Table 9-10 can be used. Bit string operands of &, |, and # must be of equal length. When bit shifting, the original length of the string is preserved, as shown in the examples.

Table 9-10. Bit String Operators

OperatorDescriptionExampleResult
|| concatenationB'10001' || B'011'10001011
& bitwise ANDB'10001' & B'01101'00001
| bitwise ORB'10001' | B'01101'11101
# bitwise XORB'10001' # B'01101'11100
~ bitwise NOT~ B'10001'01110
<< bitwise shift leftB'10001' << 301000
>> bitwise shift rightB'10001' >> 200100

The following SQL-standard functions work on bit strings as well as character strings: length, bit_length, octet_length, position, substring.

In addition, it is possible to cast integral values to and from type bit. Some examples:

44::bit(10)                    0000101100
44::bit(3)                     100
cast(-44 as bit(12))           111111010100
'1110'::bit(4)::integer        14

Note that casting to just "bit" means casting to bit(1), and so will deliver only the least significant bit of the integer.

Note: Prior to PostgreSQL 8.0, casting an integer to bit(n) would copy the leftmost n bits of the integer, whereas now it copies the rightmost n bits. Also, casting an integer to a bit string width wider than the integer itself will sign-extend on the left.


User Comments

No comments could be found for this page.

Add Comment

Please use this form to add your own comments regarding your experience with particular features of PostgreSQL, clarifications of the documentation, or hints for other users. Please note, this is not a support forum, and your IP address will be logged. If you have a question or need help, please see the faq, try a mailing list, or join us on IRC. Note that submissions containing URLs or other keywords commonly found in 'spam' comments may be silently discarded. Please contact the webmaster if you think this is happening to you in error.

In order to submit a comment, you must have a community account.

* Comment
 

* denotes required field

Privacy Policy | Project hosted by our server sponsors. | Designed by tinysofa
Copyright © 1996 – 2010 PostgreSQL Global Development Group