Bitwise Operators
Bitwise operators perform bit manipulations on INTEGER and BINARY/VARBINARY data types:
Operator | Description | Example | Result |
---|---|---|---|
&
|
AND |
12 & 4
|
4
|
|
|
OR | 32 | 3
|
35
|
#
|
XOR |
17 # 5
|
20
|
~
|
NOT | ~1
|
-2
|
<< †
|
Bitwise shift left | 1 << 4
|
16
|
>> †
|
Bitwise shift right |
8 >> 2
|
2
|
† Invalid for BINARY/VARBINARY data types
String Argument Handling
String arguments must be explicitly cast as BINARY or VARBINARY data types for all bitwise operators. For example:
=> SELECT 'xyz'::VARBINARY & 'zyx'::VARBINARY AS AND; AND ----- xyx (1 row) => SELECT 'xyz'::VARBINARY | 'zyx'::VARBINARY AS OR; OR ----- zyz (1 row)
Bitwise operators treats all string arguments as equal in length. If the arguments have different lengths, the operator function right-pads the smaller string with one or more zero bytes to equal the length of the larger string.
For example, the following statement ANDs unequal strings xyz
and zy
. Vertica right-pads string zy
with one zero byte. The last character in the result is represented accordingly, as \000
:
=> SELECT 'xyz'::VARBINARY & 'zy'::VARBINARY AS AND; AND -------- xy\000 (1 row)