The left shift operator (<<) shifts the first operand to the left by the number of digits specified by the second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.
Note
If the first operand is an int or uint (32-digit number), the number of shifts is given by a low 5 bits of the second operand. that is, the actual shift count is 0 to 31 bits.
long or ULONG (64-bit quantity), the shift count is given by the Low-order six bits of the second operand. " > If the first operand is long or ulong (64-digit number), the shift number is given by a low 6-bit of the second operand. that is, the actual shift count is 0 to 63 bits.
Any high-order bits that are not in the first operand type range after the shift are not used, and the low-order slots are filled with 0. The shift operation never causes an overflow.
<< operator (see operator ); The type of the first operand must be the user-defined type, and the type of the second operand must is int . " The user-defined type can overload << operator (see operands); The type of the first operand must be a user-defined type, and the type of the second operand must be int. When you overload the two-tuple operator, the corresponding assignment operator, if any, is also implicitly overloaded.
Example
1 classMAINCLASS112 {3 Static voidMain ()4 {5 inti =1;6 LongLG =1;7 //Shift I one bit to the left. The result is 2.8Console.WriteLine ("0x{0:x}", I <<1);9 //in binary, is 100001. Because the value of the five Low-orderTen //bits is 1 and the result of the shift is again 2. OneConsole.WriteLine ("0x{0:x}", I << -); A //Because The type of LG is long, the and the shift is the value of the six - //Low-order bits. In this example, the shift is, and the value of - //LG is shifted-bits to the left. the //in binary:10 0000 0000 0000 0000 0000 0000 0000 0000 - //In hexadecimal:2 0 0 0 0 0 0 0 0 -Console.WriteLine ("0x{0:x}", LG << -); - } + } - /* + Output: A 0x2 at 0x2 - 0x200000000 - */View Code
Comments
Note that the results given by i<<1 and i<<33 are the same because the low-order 5 bits of 1 and 33 are the same.
<< operators