Bitwise XOR

All functions > BITWISE > Bitwise XOR

Returns the bitwise XOR (exclusive OR) of two bitstrings or two integers.

Syntax

BITWISE_XOR(left, right)

Function form

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

BITWISE_XOR(left: BITSTRING, right: BITSTRING) → BITSTRING
sql
ParameterTypeRequiredDescription
leftBITSTRINGYesLeft operand
rightBITSTRINGYesRight operand

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

BITWISE_XOR(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 XORed independently: different bits = 1, same bits = 0
  • XOR is its own inverse: BITWISE_XOR(BITWISE_XOR(a, b), b) = a
  • Returns NULL if either input is NULL
  • The ^ token is numeric exponentiation in FeatureQL, not bitwise XOR—use BITWISE_XOR

Examples

FeatureQL
RETURN
    -- Different bits become 1
    f1 := BITWISE_XOR('1010'::BITSTRING, '0110'::BITSTRING)::VARCHAR,
    -- Same bits cancel out
    f2 := BITWISE_XOR('1111'::BITSTRING, '1111'::BITSTRING)::VARCHAR,
    -- XOR with zero preserves bits
    f3 := BITWISE_XOR('1010'::BITSTRING, '0000'::BITSTRING)::VARCHAR
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
110000001010

BITWISE_XOR()

FeatureQL
RETURN
    -- Different bits become 1
    f1 := BITWISE_XOR('1010'::BITSTRING, '0110'::BITSTRING)::VARCHAR
;
Result
f1 VARCHAR
1100