SHIFTLEFT
Function
Performs a left bit-shift operation on an integer value. Shifting left by n bits is equivalent to multiplying by 2n. Supports int and bigint types.
Parameters
value: int or bigint type, the value to shiftn: int type, the number of bit positions to shift; must be greater than or equal to 0
Return Value
- The same type as
value(int or bigint) - Returns the result after the left shift
- Returns NULL if
valueornis 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
- Shift operations can cause overflow:
- int range: -2,147,483,648 to 2,147,483,647
- bigint range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- If the result exceeds the type range, overflow occurs (result is undefined)
- Bit-shift operations are faster than multiplication
- Common use cases:
- Fast computation of powers of 2
- Bit flag and bit mask operations
- Data encoding and decoding
- Related functions:
- shiftright — arithmetic right shift (preserves sign bit)
- shiftrightunsigned — logical right shift (does not preserve sign bit)
