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
ParameterTypeRequiredDescription
leftBITSTRINGYesLeft operand
rightBITSTRINGYesRight operand

Returns: Result integer, or NULL if either operand is NULL

BITWISE_AND(left: BIGINT, right: BIGINT) → BIGINT
sql
ParameterTypeRequiredDescription
leftBIGINTYesLeft operand (any integer type, read as 64 bits)
rightBIGINTYesRight 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 VARCHARf2 VARCHARf3 VARCHAR
1000110100000

Operator &

FeatureQL
RETURN
    -- Integers: 1100 AND 1010
    f1 := BITWISE_AND(12, 10),
    -- -1 has every bit set
    f2 := BITWISE_AND(-1, 255)
;
Result
f1 BIGINTf2 BIGINT
8255

BITWISE_AND()

FeatureQL
RETURN
    -- AND preserves common 1-bits
    f1 := BITWISE_AND('10101'::BITSTRING, '10001'::BITSTRING)::VARCHAR
;
Result
f1 VARCHAR
10001