Skip to main content

lshift

1. LSHIFT Function

The LSHIFT function in H2 database is used to perform a left shift operation on a given numeric value.

2. Syntax

The syntax for the LSHIFT function in H2 database is as follows:

LSHIFT(value, shift)

Arguments

  • value: The numeric value to be shifted.
  • shift: The number of positions to shift the value to the left. It should be a non-negative integer.

Return

  • The LSHIFT function returns the result of left shifting the value by the specified number of positions.

3. Notes

  • The LSHIFT function in H2 database performs a bitwise left shift operation on the binary representation of the value.
  • The shift value determines the number of positions to shift the bits to the left. For each position shifted, the value is multiplied by 2.
  • If the input value is not a number or exceeds the numeric range, the LSHIFT function will return NULL.
  • Ensure that the shift value is a non-negative integer. A negative shift value will result in an error.

4. Examples

Here are a few examples demonstrating the usage of the LSHIFT function in H2 database:

Example 1 - Performing a left shift operation on a value:

SELECT LSHIFT(5, 2) AS result;

Output:

result
------
20

Explanation: In this example, the value 5 is left-shifted by 2 positions. The binary representation of 5 is 101, and left-shifting it by 2 positions results in 10100, which is equivalent to 20 in decimal form.

Example 2 - Performing a left shift operation on a column:

CREATE TABLE numbers (
id INT PRIMARY KEY,
value INT
);

INSERT INTO numbers VALUES (1, 10), (2, 7), (3, 3);

SELECT id, value, LSHIFT(value, 1) AS result
FROM numbers;

Output:

id | value | result
---+-------+-------
1 | 10 | 20
2 | 7 | 14
3 | 3 | 6

Explanation: In this example, the value column is left-shifted by 1 position for each row. The resulting values are 20, 14, and 6 respectively.

  • rshift - Perform a right shift operation.