Bitwise Functions
- reference
All Bit/Binary functions can only operate on 64-bit signed integers.
All non-integer numbers and other data types result in null. |
Couchbase Server uses two’s complement representation. |
When looking at the value in binary form, bit 1 is the Least Significant Bit (LSB) and bit 32 is the Most Significant Bit (MSB).
(MSB) Bit 32 → 0000 0000 0000 0000 0000 0000 0000 0000
← Bit 1 (LSB)
BITAND (int_value1
, int_value2
, ...)
Description
Returns the result of a bitwise AND operation performed on all input integer values.
The bitwise AND operation compares each bit of int_value1
to the corresponding bit of every other int_value
.
If all bits are 1, then the corresponding result bit is set to 1; otherwise it is set to 0 (zero).
Arguments
- int_value1, int_value2, ...
-
Integers, or any valid expressions which evaluate to integers, that are used to compare.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
BITCLEAR (int_value
, positions
)
Description
Returns the result after clearing the specified bit, or array of bits in int_value
using the given positions
.
Specifying a negative or zero bit position makes the function return a null. |
Arguments
- int_value
-
An integer, or any valid expression which evaluates to an integer, that contains the target bit or bits to clear.
- positions
-
An integer or an array of integers specifying the position or positions to be cleared.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
BITNOT (int_value
)
Description
Returns the results of a bitwise logical NOT operation performed on an integer value.
The bitwise logical NOT operation reverses the bits in the value. For each value bit that is 1, the corresponding result bit will be set to 0 (zero); and for each value bit that is 0 (zero), the corresponding result bit will be set to 1.
All bits of the integer will be altered by this operation. |
Arguments
- int_value
-
An integer, or any valid expression which evaluates to an integer, that contains the target bits to reverse.
BITOR (int_value1
, int_value2
, ...)
Description
Returns the result of a bitwise inclusive OR operation performed on all input integer values.
compares each bit of int_value1
to the corresponding bit of every other int_value
.
If any bit is 1, the corresponding result bit is set to 1; otherwise, it is set to 0 (zero).
Arguments
- int_value1, int_value2, ...
-
Integers, or any valid expressions which evaluate to integers, that are used to compare.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
BITSET (int_value
, positions
)
Description
Returns the result after setting the specified bit position
, or array of bit positions, to 1 in the given int_value
.
Specifying a negative or zero position makes the function return a null. |
Arguments
- int_value
-
An integer, or any valid expression which evaluates to an integer, that contains the target bit or bits to set.
- positions
-
An integer or an array of integers specifying the position or positions to be set.
Return Value
An integer, representing the result after setting the bit or bits specified. If the bit is already set, then it stays set.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
BITSHIFT (int_value
, shift_amount
, rotate
)
Description
Returns the result of a bit shift operation performed on the integer value int
.
The shift_amount
supports left and right shifts.
These are logical shifts.
The third parameter rotate
supports circular shift.
This is similar to the BitROTATE function in Oracle.
Arguments
- int_value
-
An integer, or any valid expression which evaluates to an integer, that contains the target bit or bits to shift.
- shift_amount
-
An integer, or any valid expression which evaluates to an integer, that contains the number of bits to shift.
A positive (+) number means this is a LEFT shift.
A negative (-) number means this is a RIGHT shift.
- rotate
-
[Optional; FALSE by default] A boolean, or any valid expression which evaluates to a boolean, where:
-
FALSE means this is a LOGICAL shift, where bits shifted off the end of a value are considered lost.
-
TRUE means this is a CIRCULAR shift (shift-and-rotate operation), where bits shifted off the end of a value are rotated back onto the value at the other end. In other words, the bits rotate in what might be thought of as a circular pattern; therefore, these bits are not lost.
For comparison, see the below table.
-
Input | Shift | Result of Logical Shift (Rotate FALSE) |
Result of Circular Shift (Rotate TRUE) |
---|---|---|---|
6 (0000 0110) |
4 |
96 (0110 0000) |
96 (0110 0000) |
6 (0000 0110) |
3 |
48 (0011 0000) |
48 (0011 0000) |
6 (0000 0110) |
2 |
24 (0001 1000) |
24 (0001 1000) |
6 (0000 0110) |
1 |
12 (0000 1100) |
12 (0000 1100) |
6 (0000 0110) |
0 |
6 (0000 0110) |
6 (0000 0110) |
6 (0000 0110) |
-1 |
3 (0000 0011) |
3 (0000 0011) |
6 (0000 0110) |
-2 |
1 (0000 0001) |
-9223372036854775807 (1000 0000 ... 0000 0001) |
6 (0000 0110) |
-3 |
0 (0000 0000) |
-4611686018427387904 (1100 0000 ... 0000 0000) |
6 (0000 0110) |
-4 |
0 (0000 0000) |
6917529027641081856 (0110 0000 ... 0000 0000) |
Return Value
An integer, representing the result of either a logical or circular shift of the given integer.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
Examples
Circular right shift of the number 6 (0110 in binary) by two bits.
n1qlSELECT BITSHIFT(6,-2,TRUE) AS BitSHIFT;
json[
{
"BitSHIFT": -9223372036854775807
}
]
This results in -9223372036854775807 (1100 0000 0000 0000 0000 0000 0000 0000 in binary) because the two 1-bits wrapped right, around to the Most Significant Digit position and changed the integer’s sign to negative.
BITTEST (int_value
, positions [, all_set]
)
This function has a synonym ISBITSET().
Description
Returns TRUE if the specified bit, or bits, is a 1; otherwise, returns FALSE if the specified bit, or bits, is a 0 (zero).
Specifying a negative or zero bit position will result in NULL being returned. |
Arguments
- int_value
-
An integer, or any valid expression which evaluates to an integer, that contains the target bit or bits to test.
- positions
-
An integer or an array of integers specifying the position or positions to be tested.
- all_set
-
[Optional; FALSE by default] A boolean, or any valid expression which evaluates to a boolean.
When
all_set
is FALSE, then it returns TRUE even if one bit in one of the positions is set.When
all_set
is TRUE, then it returns TRUE only if all input positions are set.
Return Value
A boolean, that follows the below table:
int_value |
all_set |
Return Value |
---|---|---|
all specified bits are TRUE |
FALSE |
TRUE |
all specified bits are TRUE |
TRUE |
TRUE |
some specified bits are TRUE |
FALSE |
TRUE |
some specified bits are TRUE |
TRUE |
FALSE |
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
Examples
Find only flights that have 1 layover (to rest and walk around). That is, flight stops of 1 (0001 in binary) only.
n1qlSELECT airline, stops, schedule[0].day FROM `travel-sample`.inventory.route
WHERE stops = 1;
[source,json] [ { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 } ]
The above query returns the exact same results as the below query which uses a bit operation.
n1qlSELECT airline, stops, schedule[0].day FROM `travel-sample`.inventory.route
WHERE BITTEST(stops,1);
[source,json] [ { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "FL", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 }, { "airline": "WN", "day": 0, "stops": 1 } ]
BITXOR (int_value1
, int_value2
, ...)
Description
Returns the result of a bitwise Exclusive OR operation performed on two or more integer values.
The bitwise Exclusive OR operation compares each bit of int_value1
to the corresponding bit of int_value2
.
If there are more than two input values, the first two are compared; then their result is compared to the next input value; and so on.
When the compared bits do not match, the result bit is 1; otherwise, the compared bits do match, and the result bit is 0 (zero), as summarized:
Bit 1 | Bit 2 | XOR Result Bit |
---|---|---|
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
Arguments
- int_value1, int_value2, ...
-
Integers, or any valid expressions which evaluate to integers, that are used to compare.
Limitations
Input values must be integers (such as 1 or 1.0) and cannot contain decimals (such as 1.2).
Examples
Perform the XOR operation on 3 (0011 in binary) and 6 (0110 in binary).
n1qlSELECT BITXOR(3,6) AS BitXOR;
json[
{
"BitXOR": 5
}
]
This returns 5 (0101 in binary) because the 1st bit pair and 3rd bit pair are different (resulting in 1) while the 2nd bit pair and 4th bit pair are the same (resulting in 0):
0011 (3) 0110 (6) ======== 0101 (5)
ISBITSET → see BITTEST
Synonym of BITTEST().