SHIFTLEFT
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
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)
