Bitwise operators
seekdb supports common bitwise operators. Bitwise operations are performed using BIGINT UNSIGNED and do not consider the sign bit.
Overview of bitwise operators
The following table lists the bitwise operators supported in the current version of seekdb.
| Operator | Operand type | Description |
|---|---|---|
& | Binary | Bitwise AND |
| ` | ` | Binary |
~ | Unary | 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 1 in the corresponding bit position. The result is an unsigned 64-bit integer.
Example:
SELECT 28 & 15;
+---------+
| 28 & 15 |
+---------+
| 12 |
+---------+
1 row in set (0.001 sec)
Bitwise OR |
The result is 0 only if both operands have a 0 in the corresponding bit position. The result is an unsigned 64-bit integer.
Example:
SELECT 28 | 15;
+---------+
| 28 | 15 |
+---------+
| 31 |
+---------+
1 row in set (0.001 sec)
Bitwise NOT ~
Each 0 becomes 1 and each 1 becomes 0 in the corresponding bit position. The result is an unsigned 64-bit integer.
Example:
SELECT 5 & ~1;
+--------+
| 5 & ~1 |
+--------+
| 4 |
+--------+
1 row in set (0.001 sec)
Bitwise XOR ^
The result is 0 if the corresponding bits of both operands are the same, and 1 if they are different. The result is an unsigned 64-bit integer.
Example:
SELECT 1 ^ 1;
+-------+
| 1 ^ 1 |
+-------+
| 0 |
+-------+
1 row in set (0.001 sec)
SELECT 1 ^ 0;
+-------+
| 1 ^ 0 |
+-------+
| 1 |
+-------+
1 row in set (0.000 sec)
SELECT 13 ^ 5;
+--------+
| 13 ^ 5 |
+--------+
| 8 |
+--------+
1 row in set (0.000 sec)
Logical left shift <<
The bits of both operands are shifted left by a specified number of positions. The higher-order bits are discarded, and zeros are added to the lower-order positions. 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).
SELECT 1 << 2;
+--------+
| 1 << 2 |
+--------+
| 4 |
+--------+
1 row in set (0.001 sec)
Logical right shift >>
The bits of both operands are shifted right by a specified number of positions. For unsigned numbers, zeros are added to the higher-order positions. 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).
SELECT 4 >> 2;
+--------+
| 4 >> 2 |
+--------+
| 1 |
+--------+
1 row in set (0.001 sec)