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
| Parameter | Type | Required | Description |
|---|---|---|---|
expr | BITSTRING | Yes | Input bitstring |
Returns: Bitwise complement, or NULL if the input is NULL
BITWISE_NOT(expr: BIGINT) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
expr | BIGINT | Yes | Input 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 VARCHAR | f2 VARCHAR | f3 VARCHAR |
|---|---|---|
| 0101 | 1111 | 0000 |
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 |