Bitwise left shift

All functions > BITWISE > Bitwise left shift

Returns the bitstring or integer shifted left by N positions, filling with zeros on the right.

Syntax

expr << n  ·  BITWISE_SHIFT_LEFT(expr, n)

Function form

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

BITWISE_SHIFT_LEFT(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_LEFT(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 left end are discarded
  • New bits on the right are filled with zeros
  • A bitstring keeps its length; bitstrings exist on DuckDB and Postgres only
  • An integer keeps its low 64 bits: BITWISE_SHIFT_LEFT(1, 63) is the smallest BIGINT
  • 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_LEFT(...)

FeatureQL
RETURN
    -- Shift left by 3
    f1 := BITWISE_SHIFT_LEFT('1001011'::BITSTRING, 3)::VARCHAR,
    -- Shift left by 2
    f2 := BITWISE_SHIFT_LEFT('0001'::BITSTRING, 2)::VARCHAR,
    -- High bit shifted out
    f3 := BITWISE_SHIFT_LEFT('1000'::BITSTRING, 1)::VARCHAR
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
101100001000000

Operator ‹‹

FeatureQL
RETURN
    -- Integer shift: multiply by 2^10
    f1 := BITWISE_SHIFT_LEFT(1, 10),
    -- Bits past bit 63 are dropped; bit 0 becomes the sign bit
    f2 := BITWISE_SHIFT_LEFT(3, 63)
;
Result
f1 BIGINTf2 BIGINT
1024-9223372036854776000

BITWISE_SHIFT_LEFT()

FeatureQL
RETURN
    -- Shift left by 3
    f1 := BITWISE_SHIFT_LEFT('1001011'::BITSTRING, 3)::VARCHAR
;
Result
f1 VARCHAR
1011000