OceanBase Database supports general bitwise operators. A bitwise operation uses the BIGINT UNSIGNED operation, without taking the sign bit into account.
Overview
The following table describes the bitwise operators supported by OceanBase Database.
| Operator | Operand type | Description |
|---|---|---|
& |
Binary | The bitwise AND operator. |
\| |
Binary | The bitwise OR operator. |
~ |
Unary | The bitwise NOT operator. |
^ |
Binary | The bitwise XOR operator. |
<< |
Binary | The logical left shift operator. |
>> |
Binary | The logical right shift operator. |
Description
Bitwise AND operator (&)
The result is 1 only when the binary bits of both operands are 1. The result is an unsigned 64-bit integer.
Here is an example:
obclient> SELECT 28 & 15;
+---------+
| 28 & 15 |
+---------+
| 12 |
+---------+
1 row in set
Bitwise OR operator (|)
The result is 0 only when the binary bits of both operands are 0. 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 (~)
The operator inverts all bits. It turns zeros to ones, and ones to zeros. The result is an unsigned 64-bit integer.
Here is an example:
obclient> SELECT 5 & ~1;
+--------+
| 5 & ~1 |
+--------+
| 4 |
+--------+
1 row in set
Bitwise XOR operator (^)
If the binary bits of the two operands are the same, the result is 0. Otherwise, the result is 1. 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 operator (<<)
Shifts the specified number of bits from the operand on the right to the operand on the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right. The result is an unsigned 64-bit integer. The value is truncated to 64 bits. If the shift count is greater than or equal to the width of the unsigned 64-bit number, all bits in the result are 0.
In the following example, 1 (00000001) is shifted 2 bits to the left and zeros are added to the right. The result is 4 (00000100).
obclient> SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
| 4 |
+--------+
1 row in set
Logical right shift operator (>>)
Shifts the specified number of bits from the operand on the left to the operand on the right. For unsigned numbers, the bit positions that have been vacated by the shift operation are filled with zeros. The result is an unsigned 64-bit integer. The value is truncated to 64 bits. If the shift count is greater than or equal to the width of the unsigned 64-bit number, all bits in the result are 0.
In the following example, 4 (00000100) is shifted 2 bits to the right and zeros are added to the left. The result is 1 (00000001).
obclient> SELECT 4 >> 2;
+--------+
| 4 >> 2 |
+--------+
| 1 |
+--------+
1 row in set