Bit String Functions and Operators

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PostgreSQL : Bit String Functions and Operators

Introduction

In this section, we discussed 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 are shown in the following table 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.

Operator Description
|| concatenation
& bitwise AND
| bitwise OR
# bitwise XOR
~ bitwise NOT
<< bitwise shift left
>> bitwise shift right

Example : concatenation (||) operator

postgres=# SELECT B'10001' || B'011';
 ?column?
----------
 10001011
(1 row)

Example : bitwise AND (&) operator

postgres=# SELECT B'11101' & B'01111';
 ?column?
----------
 01101
(1 row)

Example : bitwise OR (|) operator

postgres=# SELECT B'10101' | B'01011';
 ?column?
----------
 11111
(1 row)

 

Example : bitwise XOR (#) operator

postgres=# SELECT B'10101' # B'01011';
 ?column?
----------
 11110
(1 row)

Example : bitwise NOT (~) operator

postgres=# SELECT ~ B'10101';
 ?column?
----------
 01010
(1 row)

Example : bitwise shift left (<<) operator

postgres=# SELECT B'10001' << 4;
 ?column?
----------
 10000
(1 row)

Example : bitwise shift right (>>) operator

postgres=# SELECT B'10001' >> 3;
 ?column?
----------
 00010
(1 row)