Performs an arithmetic left shift on a bit pattern.
result = pattern << amount
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) |
|---|---|---|
| Byte | 7 | &H00000007 |
| Short | 15 | &H0000000F |
| Integer | 31 | &H0000001F |
| Long | 63 | &H0000003F |
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.
Bit Shift Operators | Assignment Operators | <<= Operator | Operator Precedence in Visual Basic | Operators Listed by Functionality