Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Operators
 << Operator

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
C# Language Reference
<< Operator (C# Reference)

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

If first operand is an int or uint (32-bit quantity), the shift count is given by the low-order five bits of second operand.

If first operand is a long or ulong (64-bit quantity), the shift count is given by the low-order six bits of second operand.

The high-order bits of first operand 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. When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.

// cs_operator_left_shift.cs
using System;
class MainClass
{
    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.

Reference

C# Operators

Concepts

C# Programming Guide

Other Resources

C# Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker