Reference Guide > TDV Support for SQL Functions > TDV-Supported Binary Functions > SHR Functions
 
SHR Functions
The SHR functions right-shift the bits of the binary representation of a number.
Sample Syntax
INT1SHR(arg1, arg2[, arg3])
Remarks
Shifts arg1 right by arg2 bits.
With each shift, a 0 is placed in the second-most-significant bit of the INTEGER (of whatever size), and the least significant bit is shifted out.
If arg3 is present, arg1 is ANDed with arg3 before being shifted.
Each left bit-shift doubles the number.
The most significant bit of the binary representation of arg1 acts like a sign bit. It does not move or change; that is, negative numbers remain negative, and positive numbers remain positive.
If arg1 is an odd number (whether positive or negative), the result of each position shift is (arg1 minus 1) divided by 2. If arg1 is even, the result is arg1 divided by 2.
Arg2 should be a nonnegative number (positive or 0).
Examples
The table below shows examples of SHR. Most of the examples use INT1.
Function Input
Result
Comments
INT1SHR(1,0)
1
Arg2 is 0, so no shift takes place.
INT1SHR(1,1)
0
 
INT1SHR(2,1)
1
 
INT1SHR(3,1)
1
Adjacent pairs of arg1 values map to the same result.
INT1SHR(5,1)
2
5 is odd, so the result is 5 -1 (=4) divided by 2, or 2.
INT1SHR(-5,1)
-3
-5 is odd, so the result is -5 -1 (=-6) divided by 2, or -3.
INT2SHR(127,1)
63
 
INT2SHR(127,1,6)
3
Arg3 is present. Because both the 4-bit and the 2-bit are set in 127 (0111 1111), the AND result is 6; shifted right one position it becomes 3.
INT2SHR(127,17)
63
Arg2 is 9, the same as 1 mod 16 (the number of bits in INT2), so the result is the same as INT2SHR(127,1).
INT1SHR(-128,8)
-128
Arg 2 is 8, the same as 0 mod 8, so the result is the same as INT1SHR(-128,0); that is, no shift.