Bitwise NOT

All functions > BITWISE > Bitwise NOT

Returns the bitwise NOT (complement) of a bitstring or an integer, flipping every bit.

Syntax

~expr  ·  BITWISE_NOT(expr)

Function form

Returns: Bitwise complement, or NULL if the input is NULL

BITWISE_NOT(expr: BITSTRING) → BITSTRING
sql
ParameterTypeRequiredDescription
exprBITSTRINGYesInput bitstring

Returns: Bitwise complement, or NULL if the input is NULL

BITWISE_NOT(expr: BIGINT) → BIGINT
sql
ParameterTypeRequiredDescription
exprBIGINTYesInput integer (any integer type, read as 64 bits)

Notes

  • Every 0 becomes 1 and every 1 becomes 0
  • A bitstring keeps its length; bitstrings exist on DuckDB and Postgres only
  • An integer flips all 64 bits of its two's complement, so NOT x = -x - 1
  • Returns NULL if the input is NULL

Examples

BITWISE_NOT(...)

FeatureQL
RETURN
    -- Flip all bits
    f1 := BITWISE_NOT('1010'::BITSTRING)::VARCHAR,
    -- All zeros become all ones
    f2 := BITWISE_NOT('0000'::BITSTRING)::VARCHAR,
    -- All ones become all zeros
    f3 := BITWISE_NOT('1111'::BITSTRING)::VARCHAR
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
010111110000

Operator ~

FeatureQL
RETURN
    -- Integers flip all 64 bits: NOT x = -x - 1
    f1 := BITWISE_NOT(5)
;
Result
f1 BIGINT
-6

BITWISE_NOT()

FeatureQL
RETURN
    -- Flip all bits
    f1 := BITWISE_NOT('1010'::BITSTRING)::VARCHAR
;
Result
f1 VARCHAR
0101