Bitwise right shift

All functions > BITWISE > Bitwise right shift

Returns the bitstring or integer shifted right by N positions.

Syntax

expr >> n  ·  BITWISE_SHIFT_RIGHT(expr, n)

Function form

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

BITWISE_SHIFT_RIGHT(expr: BITSTRING, n: BIGINT) → BITSTRING
sql
ParameterTypeRequiredDescription
exprBITSTRINGYesInput bitstring
nBIGINTYesNumber of bit positions to shift

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

BITWISE_SHIFT_RIGHT(expr: BIGINT, n: BIGINT) → BIGINT
sql
ParameterTypeRequiredDescription
exprBIGINTYesInput integer (any integer type, read as 64 bits)
nBIGINTYesNumber of bit positions to shift, from 0 to 63

Notes

  • Bits shifted beyond the right end are discarded
  • A bitstring keeps its length and fills with zeros on the left; bitstrings exist on DuckDB and Postgres only
  • An integer shift keeps the sign (arithmetic): BITWISE_SHIFT_RIGHT(-8, 1) is -4
  • An integer shift N must be between 0 and 63, or the query fails at runtime
  • Returns NULL if either input is NULL

Examples

BITWISE_SHIFT_RIGHT(...)

FeatureQL
RETURN
    -- Shift right by 3
    f1 := BITWISE_SHIFT_RIGHT('1001011'::BITSTRING, 3)::VARCHAR,
    -- Shift right by 2
    f2 := BITWISE_SHIFT_RIGHT('1000'::BITSTRING, 2)::VARCHAR,
    -- Low bit shifted out
    f3 := BITWISE_SHIFT_RIGHT('0001'::BITSTRING, 1)::VARCHAR
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
000100100100000

Operator ››

FeatureQL
RETURN
    -- Integer shift: divide by 2^3
    f1 := BITWISE_SHIFT_RIGHT(1024, 3),
    -- The sign is kept (arithmetic shift)
    f2 := BITWISE_SHIFT_RIGHT(-8, 1)
;
Result
f1 BIGINTf2 BIGINT
128-4

BITWISE_SHIFT_RIGHT()

FeatureQL
RETURN
    -- Shift right by 3
    f1 := BITWISE_SHIFT_RIGHT('1001011'::BITSTRING, 3)::VARCHAR
;
Result
f1 VARCHAR
0001001