OceanBase AI Database supports common bitwise operators. Bitwise operations are performed using BIGINT UNSIGNED type, so the sign bit is not considered.
Overview of bitwise operators
The following table describes the bitwise operators supported by the current version of OceanBase AI Database.
operator |
Operand type |
Meaning |
|---|---|---|
& |
binary | Bitwise AND |
\| |
binary | Bitwise OR |
~ |
unitary | Bitwise NOT |
^ |
binary | Bitwise XOR |
<< |
binary | Logical Left Shift |
>> |
binary | Logical Right Shift |
Description
Bitwise AND &
The result is 1 only if both operands have a binary 1 in the corresponding bit position. The result is an unsigned 64-bit integer.
An example is as follows:
obclient> SELECT 28 & 15;
+---------+
| 28 & 15 |
+---------+
| 12 |
+---------+
1 row in set
Bitwise OR |
The result is 0 only when both operands have a binary 0 bit. The result is an unsigned 64-bit integer.
Here is an example:
obclient> SELECT 28 | 15;
+---------+
| 28 | 15 |
+---------+
| 31 |
+---------+
1 row in set
Bitwise NOT operator ~
Zeros in the binary representation are changed to ones, and ones are changed to zeros. The result is an unsigned 64-bit integer.
An example is as follows:
obclient> SELECT 5 & ~1;
+--------+
| 5 & ~1 |
+--------+
| 4 |
+--------+
1 row in set
Bitwise XOR ^
A binary digit is 0 if the corresponding bits of the two operands are the same, and 1 if they are different. The result is an unsigned 64-bit integer.
Here is an example:
obclient> SELECT 1 ^ 1;
+-------+
| 1 ^ 1 |
+-------+
| 0 |
+-------+
1 row in set
obclient> SELECT 1 ^ 0;
+-------+
| 1 ^ 0 |
+-------+
| 1 |
+-------+
1 row in set
obclient> SELECT 13 ^ 5;
+--------+
| 13 ^ 5 |
+--------+
| 8 |
+--------+
1 row in set
Logical Left Shift <<
Both operands are shifted left by the specified number of bits. The high-order bits are discarded, and 0s are inserted in the low-order positions. The result is an unsigned 64-bit integer. This value is truncated to 64 bits. If the shift count is greater than or equal to the width of an unsigned 64-bit number, the result is zero.
The following example shows shifting the binary number 1 (00000001) left by 2 bits and then padding zeros on the right to obtain 4 (00000100).
obclient> SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
| 4 |
+--------+
1 row in set
Logical Right Shift >>
Both operands are shifted right by the specified number of bits. For unsigned numbers, 0s are padded in the leftmost positions. The result is an unsigned 64-bit integer, which is truncated to 64 bits. If the shift count is greater than or equal to the width of a 64-bit unsigned number, the result is zero.
In the following example, 4 (00000100) is shifted right by 2 bits and padded with 0s on the left, resulting in 1 (00000001).
obclient> SELECT 4 >> 2;
+--------+
| 4 >> 2 |
+--------+
| 1 |
+--------+
1 row in set