<< Operator (Visual Basic)
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
Required. Integral numeric expression. The bit pattern to be shifted. The data type must be an integral type (
Required. Numeric expression. The number of bits to shift the bit pattern. The data type must be
Integer or widen to
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 a shift by more bits than the result can hold, Visual Basic masks the value of
amount with a size mask that corresponds 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
||Size mask (decimal)||Size mask (hexadecimal)|
amount is zero, the value of
result is identical to the value of
amount is negative, it is taken as an unsigned value and masked with the appropriate size mask.
Arithmetic shifts never generate overflow exceptions.
<< operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure that you understand its redefined behavior. For more information, see Operator Procedures.
The following 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 ' The bit pattern is 0000 0000 1100 0000. Dim result1, result2, result3, result4, result5 As Short result1 = pattern << 0 result2 = pattern << 4 result3 = pattern << 9 result4 = pattern << 17 result5 = pattern << -1
The results of the previous example are as follows:
result1is 192 (0000 0000 1100 0000).
result2is 3072 (0000 1100 0000 0000).
result3is -32768 (1000 0000 0000 0000).
result4is 384 (0000 0001 1000 0000).
result5is 0 (shifted 15 places to the left).
The shift amount for
result4 is calculated as 17 AND 15, which equals 1.