bit_shift_right
Shifts the binary representation of a numeric expression to the right by a specified number of bits.
This function performs an arithmetic right shift, during which the bit length does not change, the low bit is dropped, and the sign bit is used as the high bit. For example, shifting 10101
to the right by one bit results in 11010
.
Syntaxβ
bit_shift_right(value, shift)
Parametersβ
value
: the value or numeric expression to shift. Supported data types are TINYINT, SMALLINT, INT, BIGINT, and LARGEINT.
shift
: the number of bits to shift. The supported data type is BIGINT. The supported data type is BIGINT.
Return valueβ
Returns a value of the same type as value
.
Usage notesβ
- If any input parameter is NULL, NULL is returned.
- If
shift
is less than 0, 0 is returned. - Shifting a
value
by0
always results in the originalvalue
. - Shifting
0
by ashift
always results in0
. - If the data type of
value
is numeric but not an integer, that value will be cast to an integer. See Examples. - If the data type of
value
is a string, the value will be cast to an integer if possible. For example, the string "2.3" will be cast to 2. If the value cannot be cast to an integer, the value will be treated as NULL. See Examples.
Examplesβ
Use this function to shift numeric values.
SELECT bit_shift_right(2, 1);
+-----------------------+
| bit_shift_right(2, 1) |
+-----------------------+
| 1 |
+-----------------------+
SELECT bit_shift_right(2.2, 1);
+-------------------------+
| bit_shift_right(2.2, 1) |
+-------------------------+
| 1 |
+-------------------------+
SELECT bit_shift_right("2", 1);
+-------------------------+
| bit_shift_right('2', 1) |
+-------------------------+
| 1 |
+-------------------------+
SELECT bit_shift_right(-2, 1);
+------------------------+
| bit_shift_right(-2, 1) |
+------------------------+
| -1 |
+------------------------+