Help Center/ AI DataLake/ Aura reference/ Functions and Operators/ Bit String Functions and Operators
Updated on 2026-11-06 GMT+08:00

Bit String Functions and Operators

Aside from the usual comparison operators, the following operators can be used. Bit string operands of &, |, and # must be of equal length. When bit shifting, the original length of the string is preserved by zero padding (if necessary).

||

Description: Connects bit strings.

For example:

1
2
3
4
5
postgres=# SELECT B'10001' || B'011' AS RESULT;
  result
----------
 10001011
(1 row)

&

Description: AND operation between bit strings

For example:

1
2
3
4
5
postgres=# SELECT B'10001' & B'01101' AS RESULT;
 result 
--------
 00001
(1 row)

|

Description: OR operation between bit strings

For example:

1
2
3
4
5
postgres=# SELECT B'10001' | B'01101' AS RESULT;
 result 
--------
 11101
(1 row)

#

Description: OR operation between bit strings if they are inconsistent. If the same positions in the two bit strings are both 1 or 0, the position returns 0.

For example:

1
2
3
4
5
postgres=# SELECT B'10001' # B'01101' AS RESULT;
 result 
--------
 11100
(1 row)

~

Description: NOT operation between bit strings

For example:

1
2
3
4
5
postgres=# SELECT ~B'10001'AS RESULT;
 result  
----------
 01110
(1 row)

<<

Description: Binary left shift.

For example:
1
2
3
4
5
postgres=# SELECT B'10001' << 3 AS RESULT;
 result  
----------
 01000
(1 row)

>>

Description: binary right shift

For example:

1
2
3
4
5
postgres=# SELECT B'10001' >> 2 AS RESULT;
 result  
----------
 00100
(1 row)

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