SHIFTLEFT

shiftleft(value, n)

Description

Performs a left bitwise shift on an integer value. Shifting left by n bits is equivalent to multiplying by 2n. This function supports both int and bigint types.

Parameters

  • value: int or bigint type, the value to be shifted
  • n: int type, the number of bits to shift, must be greater than or equal to 0

Returns

  • Same type as value (int or bigint)
  • Returns the result after the left shift
  • If value or n is NULL, returns NULL

Examples

SELECT shiftleft(1, 2);
-- Result: 4
SELECT shiftleft(8, 2);
-- Result: 32
SELECT shiftleft(0, 2);
-- Result: 0
SELECT shiftleft(1L, 2);
-- Result: 4
SELECT shiftleft(8L, 2);
-- Result: 32
SELECT shiftleft(NULL, 2);
-- Result: NULL
SELECT shiftleft(1, NULL);
-- Result: NULL
SELECT shiftleft(-4, 2);
-- Result: -16
SELECT shiftleft(1, 10);  -- 210 = 1024
-- Result: 1024

Notes

  • Shifting left by n bits is equivalent to multiplying by 2n
  • For positive numbers: shiftleft(x, n) = x * 2n
  • For negative numbers, the left shift preserves the sign bit.
  • The shift operation may cause overflow:
    • int type range: -2,147,483,648 to 2,147,483,647
    • bigint type range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • If the shifted result exceeds the type range, overflow occurs (the result is unpredictable).
  • Bit shift operations are bitwise operations and have better performance than multiplication.
  • Common use cases:
    • Fast computation of powers of 2
    • Bit flags and bitmask operations
    • Data encoding and decoding
  • Related functions:
    • shiftright - Arithmetic right shift (preserves the sign bit)
    • shiftrightunsigned - Logical right shift (does not preserve the sign bit)