Bitwise AND
All functions > BITWISE > Bitwise AND
Returns the bitwise AND of two bitstrings or two integers.
Syntax
left & right · BITWISE_AND(left, right)
Function form
Returns: Result bitstring, or NULL if either operand is NULL
BITWISE_AND(left: BITSTRING, right: BITSTRING) → BITSTRING sql
| Parameter | Type | Required | Description |
|---|---|---|---|
left | BITSTRING | Yes | Left operand |
right | BITSTRING | Yes | Right operand |
Returns: Result integer, or NULL if either operand is NULL
BITWISE_AND(left: BIGINT, right: BIGINT) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
left | BIGINT | Yes | Left operand (any integer type, read as 64 bits) |
right | BIGINT | Yes | Right operand (any integer type, read as 64 bits) |
Notes
- Bitstring operands must have the same length; bitstrings exist on DuckDB and Postgres only
- Integers are 64-bit two's complement and the result is a BIGINT, on every backend
- Each bit position is ANDed independently: 1 AND 1 = 1, all other combinations = 0
- Returns NULL if either input is NULL
Examples
BITWISE_AND(...)
FeatureQL
RETURN
-- AND preserves common 1-bits
f1 := BITWISE_AND('10101'::BITSTRING, '10001'::BITSTRING)::VARCHAR,
-- Masking with AND
f2 := BITWISE_AND('1111'::BITSTRING, '1010'::BITSTRING)::VARCHAR,
-- No common bits
f3 := BITWISE_AND('1100'::BITSTRING, '0011'::BITSTRING)::VARCHAR
;Result
| f1 VARCHAR | f2 VARCHAR | f3 VARCHAR |
|---|---|---|
| 10001 | 1010 | 0000 |
Operator &
FeatureQL
RETURN
-- Integers: 1100 AND 1010
f1 := BITWISE_AND(12, 10),
-- -1 has every bit set
f2 := BITWISE_AND(-1, 255)
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| 8 | 255 |
BITWISE_AND()
FeatureQL
RETURN
-- AND preserves common 1-bits
f1 := BITWISE_AND('10101'::BITSTRING, '10001'::BITSTRING)::VARCHAR
;Result
| f1 VARCHAR |
|---|
| 10001 |