OceanBase Database supports the commonly used bitwise operators. Bitwise operations are performed using BIGINT UNSIGNED, and the sign bit is not considered.
Overview
The following table describes the bitwise operators supported in the current version of OceanBase Database.
| Operator | Operand type | Description |
|---|---|---|
& |
Binary | Bitwise AND |
| |
Binary | Bitwise OR |
~ |
Unary | Bitwise NOT |
^ |
Binary | Bitwise XOR |
<< |
Binary | Logical left shift |
>> |
Binary | Logical right shift |
Syntax
Bitwise AND (&)
The result is 1 only when both operands have a 1 in the corresponding bit position. The result is an unsigned 64-bit integer.
Example:
obclient> SELECT 28 & 15;
+---------+
| 28 & 15 |
+---------+
| 12 |
+---------+
1 row in set
Bitwise OR (|)
The result is 0 only when both operands have a 0 in the corresponding bit position. The result is an unsigned 64-bit integer.
Example:
obclient> SELECT 28 | 15;
+---------+
| 28 | 15 |
+---------+
| 31 |
+---------+
1 row in set
Bitwise NOT (~)
The bits are flipped: 0 becomes 1, and 1 becomes 0. The result is an unsigned 64-bit integer.
Example:
obclient> SELECT 5 & ~1;
+--------+
| 5 & ~1 |
+--------+
| 4 |
+--------+
1 row in set
Bitwise XOR (^)
The result is 0 when the corresponding bits of the two operands are the same, and 1 when they are different. The result is an unsigned 64-bit integer.
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 (<<)
The bits of the two operands are shifted left by a specified number of positions. The higher-order bits are discarded, and zeros are added to the lower-order bits. The result is an unsigned 64-bit integer. If the shift count is greater than or equal to the width of an unsigned 64-bit number, the result is zero.
For example, shifting 1 (00000001) left by 2 positions results in 4 (00000100).
obclient> SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
| 4 |
+--------+
1 row in set
Logical right shift (>>)
The bits of the two operands are shifted right by a specified number of positions. For unsigned numbers, zeros are added to the higher-order bits. The result is an unsigned 64-bit integer. If the shift count is greater than or equal to the width of an unsigned 64-bit number, the result is zero.
For example, shifting 4 (00000100) right by 2 positions results in 1 (00000001).
obclient> SELECT 4 >> 2;
+--------+
| 4 >> 2 |
+--------+
| 1 |
+--------+
1 row in set
