Bitwise Function Usage Guide
GBase 8a MPP Cluster uses the BIGINT (64-bit) algorithm for bitwise operations. Therefore, the maximum valid range for these operators is 64 bits.
Note: Bitwise operations only support numerical types.
1. |
(Bitwise OR)
Example
Returns the result of 29 | 15
.
gbase> SELECT 29 | 15 FROM dual;
+---------+
| 29 | 15 |
+---------+
| 31 |
+---------+
1 row in set
Explanation: The bit value of 29 is 11101
, and the bit value of 15 is 1111
. Performing a bitwise OR operation results in 11111
, which corresponds to the decimal value 31
.
2. &
(Bitwise AND)
Example
Returns the result of 29 & 15
.
gbase> SELECT 29 & 15 FROM dual;
+---------+
| 29 & 15 |
+---------+
| 13 |
+---------+
1 row in set
Explanation: The bit value of 29
is 11101
, and that of 15
is 1111
. Performing a bitwise AND operation results in 1101
, which corresponds to the decimal value 13
.
3. ^
(Bitwise XOR)
Examples
Example 1: Returns the result of 1 ^ 1
.
gbase> SELECT 1 ^ 1 FROM dual;
+-------+
| 1 ^ 1 |
+-------+
| 0 |
+-------+
1 row in set
Example 2: Returns the result of 1 ^ 0
.
gbase> SELECT 1 ^ 0 FROM dual;
+-------+
| 1 ^ 0 |
+-------+
| 1 |
+-------+
1 row in set
Example 3: Returns the result of 11 ^ 3
.
gbase> SELECT 11 ^ 3 FROM dual;
+--------+
| 11 ^ 3 |
+--------+
| 8 |
+--------+
1 row in set
Explanation: The bit value of 11
is 1011
, and that of 3
is 0011
. Performing a bitwise XOR operation results in 1000
, which corresponds to the decimal value 8
.
4. <<
(Left Shift - BIGINT)
Example
Returns the result of 1 << 2
.
gbase> SELECT 1 << 2 FROM dual;
+--------+
| 1 << 2 |
+--------+
| 4 |
+--------+
1 row in set
Explanation: The bit value of 1
is 0001
. Shifting two positions to the left results in 0100
, which corresponds to the decimal value 4
.
5. >>
(Right Shift - BIGINT)
Example
Returns the result of 4 >> 2
.
gbase> SELECT 4 >> 2 FROM dual;
+--------+
| 4 >> 2 |
+--------+
| 1 |
+--------+
1 row in set
Explanation: The bit value of 4
is 0100
. Shifting two positions to the right results in 0001
, which corresponds to the decimal value 1
.
6. BIT_COUNT(N)
Function Description: Returns the total number of bits set to 1 in the parameter N
.
Example
Returns the number of bits set to 1 in 29
.
gbase> SELECT BIT_COUNT(29) FROM dual;
+---------------+
| BIT_COUNT(29) |
+---------------+
| 4 |
+---------------+
1 row in set
Explanation: The bit value of 29
is 11101
, and the number of bits set to 1 is 4
.
These are the main bitwise functions in GBase 8a MPP Cluster. Thank you for reading!