Performs an arithmetic right shift on a bit pattern.
result = pattern >> amount
- Required. Integral numeric value. The result of shifting the bit pattern. The data type is the same as that of pattern.
- Required. Integral numeric expression. The bit pattern to be shifted. The data type must be Byte, Short, Integer, or Long.
- Required. Numeric expression. The number of bits to shift the bit pattern. The data type must be Integer or widen to Integer.
Arithmetic shifts are not circular, which means the bits shifted off one end of the result are not reintroduced at the other end. In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left. This means that if pattern has a negative value, the vacated positions are set to one; otherwise they are set to zero.
Note that data type Byte is considered unsigned, so there is no sign bit to propagate. If pattern is of type Byte, the vacated positions are always set to zero.
To prevent shifting by more bits than the result can hold, Visual Basic masks the value of amount with a size mask corresponding to the data type of pattern. The binary AND of these values is used for the shift amount. The size masks are as follows:
|Data type of pattern||Size mask (decimal)||Size mask (hexadecimal)|
If amount is zero, the value of result is identical to the value of pattern. If amount is negative, it is taken as an unsigned value and masked with the appropriate size mask.
Arithmetic shifts never generate overflow exceptions.
This example uses the >> operator to perform arithmetic right shifts on integral values. The result always has the same data type as that of the expression being shifted.
Dim Pattern As Short = 2560 ' Bit pattern is 0000 1010 0000 0000. Dim Result1, Result2, Result3, Result4, Result5 As Short Result1 = Pattern >> 0 ' Result is 2560 (0000 1010 0000 0000). Result2 = Pattern >> 4 ' Result is 160 (0000 0000 1010 0000). Result3 = Pattern >> 10 ' Result is 2 (0000 0000 0000 0010). Result4 = Pattern >> 18 ' Result is 640 (0000 0010 1000 0000). Result5 = Pattern >> -1 ' Result is 0 (shifted 15 places to right).
The shift amount for
Result4 is calculated as 18 AND 15, which equals 2.
Dim NegPattern As Short = -8192 ' Bit pattern is 1110 0000 0000 0000. Dim NegResult1, NegResult2 As Short NegResult1 = NegPattern >> 4 ' Result is -512 (1111 1110 0000 0000). NegResult2 = NegPattern >> 13 ' Result is -1 (sign bit propagated).