Bitwise operators and functions
Use these operators and the xor function to combine, invert, and shift the bits of integers and bitstrings. To combine the bits of many rows, see Boolean and bitwise aggregate functions.
& (bitwise AND) operator
Compares the bits of x and y, and returns a value whose bits are 1 only where both operands have a 1 bit. For example, 12 & 10 returns 8: 12 is 1100 in binary, 10 is 1010, and only the bit with the value 8 is set in both. If either operand is NULL, the result is NULL.
You can also pass two bitstrings of the same length, and the result is a bitstring of that length. A bitstring is a BIT value, such as '1010'::BIT, which casts the string '1010' to BIT. Use & with a mask to test or keep selected bits, as in (.flags & 4) <> 0
Syntax
x & y
Arguments
The & (bitwise AND) operator takes the following arguments:
x-
The first value whose bits are compared, as a value of any integer type, such as
INTEGERorBIGINT, or aBITvalue. y-
The second value whose bits are compared, of the same kind as
x. TwoBITvalues must have the same length.
Return type
The common integer type of the operands, for example INTEGER, or BIT for two BIT values.
Examples
The following SQL query returns the bitwise AND of two integers.
SELECT 91 & 15 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the bitwise AND of 12 and 10, and shows that a NULL operand returns NULL.
SELECT 12 & 10 AS integers, 5 & NULL AS null_operand
The query returns the following result.
| integers | null_operand |
|---|---|
|
|
The following SQL query returns the bitwise AND of two bitstrings.
SELECT '1010'::BIT & '0110'::BIT AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that | and & are applied from left to right, and uses parentheses to apply & first.
SELECT 4 | 1 & 2 AS left_to_right, 4 | (1 & 2) AS grouped
The query returns the following result.
| left_to_right | grouped |
|---|---|
|
|
Considerations
& accepts only integers and bitstrings. A DECIMAL or DOUBLE operand, as in 5 & 1.5, fails, so cast it to an integer type first. Two BIT values of different lengths also make the query fail.
&, |, <<, and >> have the same precedence, are applied from left to right, and are applied after + and -. For example, 4 | 1 & 2 returns 0, which is (4 | 1) & 2. In many programming languages, the same expression returns 4. Use parentheses to group bitwise operations.
Differences from Athena engine version 3
Athena engine version 3 computes a bitwise AND with the function bitwise_and(. DuckDB does not have x, y)bitwise_and, so write instead.x & y
<< (left shift) operator
Returns x with its bits moved n places to the left, with zeros added on the right. For an integer, this multiplies x by 2 to the power n, so 1 << 4 returns 16. If either operand is NULL, the result is NULL.
For a BIT value, the bitstring keeps its length. Bits that move past the left end are dropped, and zeros fill in from the right, so '1011'::BIT << 1 returns 0110.
Syntax
x << n
Arguments
The << (left shift) operator takes the following arguments:
x-
The value whose bits to shift, as a value of any integer type, such as
INTEGERorBIGINT, or aBITvalue. n-
The number of places to shift, as a value of any integer type. When
xis aBITvalue,nis anINTEGER.
Return type
The common integer type of x and n, for example INTEGER or BIGINT, or BIT for a BIT value.
Examples
The following SQL query shifts the bits of 1 four places to the left.
SELECT 1 << 4 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shifts the bits of 1 to the left by 40 places. It casts 1 to BIGINT first, because the result does not fit in an INTEGER.
SELECT CAST(1 AS BIGINT) << 40 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shifts a bitstring one place to the left. The bitstring keeps its length.
SELECT '1011'::BIT << 1 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that + is applied before <<.
SELECT 1 << 2 + 1 AS result
The query returns the following result.
| result |
|---|
|
Considerations
The query fails if the result does not fit in the result type. 1 << 31 fails, because 1 is an INTEGER literal. Cast x to a wider type, as in CAST(1 AS BIGINT) << 31.
The query also fails if an integer x is negative, as in -1 << 2, or if n is negative.
+ and - are applied before <<, so 1 << 2 + 1 returns 8, which is 1 << 3.
Differences from Athena engine version 3
Athena engine version 3 shifts bits to the left with the function bitwise_left_shift(. DuckDB does not have x, n)bitwise_left_shift, so write instead.x << n
In Athena engine version 3, bitwise_left_shift drops the bits that move past the left end of the type, so bitwise_left_shift(1, 31) returns -2147483648. In DuckDB, 1 << 31 fails, because the result does not fit in an INTEGER. Cast x to BIGINT first, as in CAST(1 AS BIGINT) << 31, which returns 2147483648.
>> (right shift) operator
Returns x with its bits moved n places to the right. Bits that move past the right end are dropped. For an integer, this divides x by 2 to the power n and rounds down, so 256 >> 4 returns 16. A negative integer stays negative, so -16 >> 2 returns -4. If either operand is NULL, the result is NULL.
For a BIT value, the bitstring keeps its length, and zeros fill in from the left, so '1011'::BIT >> 1 returns 0101.
Syntax
x >> n
Arguments
The >> (right shift) operator takes the following arguments:
x-
The value whose bits to shift, as a value of any integer type, such as
INTEGERorBIGINT, or aBITvalue. n-
The number of places to shift, as a value of any integer type. When
xis aBITvalue,nis anINTEGER.
Return type
The common integer type of x and n, for example INTEGER or BIGINT, or BIT for a BIT value.
Examples
The following SQL query shifts the bits of 8 two places to the right.
SELECT 8 >> 2 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shifts a positive and two negative integers. A negative value stays negative, and the result is rounded down.
SELECT 256 >> 4 AS positive, -16 >> 2 AS negative, -17 >> 2 AS rounded_down
The query returns the following result.
| positive | negative | rounded_down |
|---|---|---|
|
|
|
The following SQL query shifts a bitstring one place to the right. The bitstring keeps its length.
SELECT '1011'::BIT >> 1 AS result
The query returns the following result.
| result |
|---|
|
Considerations
When n is negative, or is at least the number of bits in the type, the result is 0, as in 1 >> 40.
+ and - are applied before >>, so 64 >> 1 + 1 returns 16, which is 64 >> 2.
Differences from Athena engine version 3
Athena engine version 3 shifts bits to the right with the functions bitwise_right_shift and bitwise_right_shift_arithmetic. DuckDB does not have these functions, so write instead.x >> n
In Athena engine version 3, bitwise_right_shift fills the bits on the left with zeros, so it returns a positive number for a negative x. In DuckDB, >> keeps the sign, as bitwise_right_shift_arithmetic does in Athena engine version 3, so -16 >> 2 returns -4.
| (bitwise OR) operator
Compares the bits of x and y, and returns a value whose bits are 1 where either operand has a 1 bit. For example, 12 | 10 returns 14: 12 is 1100 in binary, 10 is 1010, and the result is 1110. If either operand is NULL, the result is NULL.
You can also pass two BIT values of the same length, and the result is a bitstring of that length. Use | to set selected bits, as in . To concatenate strings, use flags | 4||.
Syntax
x | y
Arguments
The | (bitwise OR) operator takes the following arguments:
x-
The first value whose bits are compared, as a value of any integer type, such as
INTEGERorBIGINT, or aBITvalue. y-
The second value whose bits are compared, of the same kind as
x. TwoBITvalues must have the same length.
Return type
The common integer type of the operands, for example INTEGER, or BIT for two BIT values.
Examples
The following SQL query returns the bitwise OR of two integers.
SELECT 32 | 3 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the bitwise OR of two integers and of two bitstrings.
SELECT 12 | 10 AS integers, '1010'::BIT | '0110'::BIT AS bitstrings
The query returns the following result.
| integers | bitstrings |
|---|---|
|
|
The following SQL query shows that | and << are applied from left to right, and uses parentheses to shift first.
SELECT 1 | 2 << 1 AS left_to_right, 1 | (2 << 1) AS grouped
The query returns the following result.
| left_to_right | grouped |
|---|---|
|
|
Considerations
| accepts only integers and bitstrings. A DECIMAL or DOUBLE operand fails, so cast it to an integer type first. Two BIT values of different lengths also make the query fail.
|, &, <<, and >> have the same precedence and are applied from left to right. For example, 1 | 2 << 1 returns 6, which is (1 | 2) << 1. Use parentheses to group bitwise operations.
Differences from Athena engine version 3
Athena engine version 3 computes a bitwise OR with the function bitwise_or(. DuckDB does not have x, y)bitwise_or, so write instead.x | y
~ (bitwise NOT and regular expression match) operator
Returns x with every bit inverted: each 0 bit becomes 1, and each 1 bit becomes 0. For a signed integer, the result is -, so x - 1~12 returns -13. For an unsigned integer, the result depends on the size of the type, so ~CAST(0 AS UTINYINT) returns 255. For a BIT value, the result is a bitstring of the same length. If x is NULL, the result is NULL.
Between two strings, ~ is a regular expression match instead: returns string ~ patterntrue if pattern matches any part of string, the same as regexp_matches(. The string, pattern)!~ operator returns the opposite result.
Syntax
~ x
string ~ pattern
string !~ pattern
Arguments
The ~ (bitwise NOT and regular expression match) operator takes the following arguments:
x-
The value whose bits to invert, as a value of any integer type, such as
INTEGER,BIGINT, orUTINYINT, or aBITvalue. string-
The string to search, as a
VARCHARvalue. This argument applies to the regular expression form. pattern-
The regular expression to look for in
string, as aVARCHARvalue. This argument applies to the regular expression form.
Return type
The type of x, for example INTEGER, UTINYINT, or BIT. The regular expression form returns BOOLEAN.
Examples
The following SQL query inverts the bits of an integer.
SELECT ~12 AS result
The query returns the following result.
| result |
|---|
|
The following SQL query inverts the bits of a signed integer, an unsigned integer, and a bitstring.
SELECT ~15 AS signed_integer, ~CAST(0 AS UTINYINT) AS unsigned_integer, ~'1010'::BIT AS bitstring
The query returns the following result.
| signed_integer | unsigned_integer | bitstring |
|---|---|---|
|
|
|
The following SQL query shows that ~ applies to the whole sum that follows it, and uses parentheses to invert only 12.
SELECT ~12 + 1 AS without_parentheses, (~12) + 1 AS with_parentheses
The query returns the following result.
| without_parentheses | with_parentheses |
|---|---|
|
|
The following SQL query uses ~ between two strings as a regular expression match. The pattern matches any part of the string unless you anchor it.
SELECT 'abc' ~ 'a.c' AS whole_match, 'xabcx' ~ 'a.c' AS partial_match, 'xabcx' ~ '^a.c$' AS anchored
The query returns the following result.
| whole_match | partial_match | anchored |
|---|---|---|
|
|
|
Considerations
~ is applied after +, -, and *, so ~12 + 1 returns -14, which is ~(12 + 1). Parentheses around the operand alone do not change this. To get -12, write (~12) + 1.
The regular expression form matches anywhere in the string, so 'xabcx' ~ 'a.c' returns true. To match the whole string, anchor the pattern with ^ and $, as in 'xabcx' ~ '^a.c$', or use the regexp_full_match function. The match is case-sensitive.
xor function
Returns the bitwise exclusive OR of left and right. Each bit of the result is 1 when exactly one of the two matching input bits is 1, and 0 otherwise. The function works on the two's complement form of each integer, so it also combines negative numbers bit by bit. If either argument is NULL, the result is NULL.
xor is a function, not an operator. For bitwise AND and OR, use the & and | operators. To combine the values of many rows with XOR, use the bit_xor aggregate function.
Syntax
xor(left, right)
Arguments
The xor function takes the following arguments:
left-
The first value whose bits are compared, as a value of any integer type, signed or unsigned, or a
BITvalue. right-
The second value whose bits are compared, of the same kind as
left. TwoBITvalues must have the same length.
Return type
The common type of the two arguments, for example INTEGER for two INTEGER values, or BIT for two BIT values.
Examples
The following SQL query returns the bitwise exclusive OR of 17 (binary 10001) and 5 (binary 00101), which is 20 (binary 10100).
SELECT xor(17, 5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the bitwise exclusive OR of two bitstrings of the same length.
SELECT xor( CAST('1100' AS BIT), CAST('1010' AS BIT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a negative number. In two's complement, -1 has every bit set, so the result is every bit of 5 flipped, which is -6.
SELECT xor(-1, 5) AS result
The query returns the following result.
| result |
|---|
|
Considerations
xor does not accept BOOLEAN values. For the exclusive OR of two conditions, use .condition1 <> condition2
xor does not accept DECIMAL, FLOAT, or DOUBLE values. Cast them to an integer type first.
Two BIT values of different lengths make the query fail.
Differences from Athena engine version 3
Athena engine version 3 names this function bitwise_xor. DuckDB has no bitwise_xor function, so use xor.