Export (0) Print
Expand All

<< Operator

The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand.

expr << count

Where:

expr
An expression of type int, uint, long, or ulong; the value to be shifted.
count
An expression of type int; the shift count.

Remarks

If expr is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of count (count & 0x1f).

If expr is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of count (count & 0x3f).

The high-order bits of expr are discarded and the low-order empty bits are zero-filled. Shift operations never cause overflows.

User-defined types can overload the << operator (see operator); the type of the first operand must be the user-defined type, and the type of the second operand must be int.

Example

// cs_operator_left_shift.cs
using System;
class Test 
{
   public static void Main() 
   {
      int i = 1;
      long lg = 1;
      Console.WriteLine("0x{0:x}", i << 1);
      Console.WriteLine("0x{0:x}", i << 33);
      Console.WriteLine("0x{0:x}", lg << 33);
   }
}

Output

0x2
0x2
0x200000000

Note that i<<1 and i<<33 give the same result, because 1 and 33 have the same low-order five bits.

See Also

C# Operators | 7.8 Shift operators | >> Operator

Show:
© 2014 Microsoft