RIGHT_SHIFT (Transact SQL)
RIGHT_SHIFT takes two parameters, and returns the first parameter bit-shifted right by the number of bits specified in the second parameter.
The RIGHT_SHIFT function is also accessible through the
RIGHT_SHIFT ( expression_value, shift_amount ) expression_value >> shift_amount
Any integer or binary expression that isn't a large object (LOB).
The number of bits by which expression_value should be shifted. shift_amount must be an integer type.
Returns the same type as expression_value.
The shift_amount parameter is cast to a bigint. The parameter can be positive or negative, and can also be greater than the number of bits in the datatype of expression_value. When shift_amount is negative, the shift operates in the opposite direction. For example,
LEFT_SHIFT (expr, -1) is the same as
RIGHT_SHIFT (expr, 1). When shift_amount is greater than the number of bits in expression_value, the result returned will be
RIGHT_SHIFT performs a logical shift. After bits are shifted, any vacant positions will be filled by
0, regardless of whether the original value was positive or negative.
In the initial implementation, Distributed Query functionality for the bit manipulation functions within linked server or ad hoc queries (OPENQUERY) won't be supported.
In the following example, the integer value 12345 is right-shifted by 5 bits.
SELECT RIGHT_SHIFT(12345, 5);
The result is 385. If you convert 12345 to binary, you have
0011 0000 0011 1001. Shifting this to the right by 5 becomes
0001 1000 0001, which is
385 in decimal.
The following table demonstrates what happens during each shift.
|Integer value||Binary value||Description|
||Shift right by 1|
||Shift right by 2|
||Shift right by 3|
||Shift right by 4|
||Shift right by 5|