|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Performs an arithmetic left 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 left shift, the bits shifted beyond the range of the result data type are discarded, and the bit positions vacated on the right are 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 left shifts on integral values. The result always has the same data type as that of the expression being shifted.
Dim Pattern As Short = 192 ' Bit pattern is 0000 0000 1100 0000. Dim Result1, Result2, Result3, Result4, Result5 As Short Result1 = Pattern << 0 ' Result is 192 (0000 0000 1100 0000). Result2 = Pattern << 4 ' Result is 3072 (0000 1100 0000 0000). Result3 = Pattern << 9 ' Result is -32768 (1000 0000 0000 0000). Result4 = Pattern << 17 ' Result is 384 (0000 0001 1000 0000). Result5 = Pattern << -1 ' Result is 0 (shifted 15 places to left).
The shift amount for
Result4 is calculated as 17 AND 15, which equals 1.