>> Operator (C# Reference)

Switch View :
ScriptFree
Visual Studio 2010 - Visual C#
>> Operator (C# Reference)

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

Remarks

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

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

If the first operand is an int or long, the right-shift is an arithmetic shift (high-order empty bits are set to the sign bit). If the first operand is of type uint or ulong, the right-shift is a logical shift (high-order bits are zero-filled).

User-defined types can overload the >> operator; the type of the first operand must be the user-defined type, and the type of the second operand must be int. For more information, see operator. When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.

Example

C#

class RightShift
{
    static void Main()
    {
        int i = -1000;
        Console.WriteLine(i >> 3);
    }
}
/*
Output:
-125
*/


See Also

Reference

Concepts

Other Resources

Community Content

JMCF125
Using shift operators with 16 bit integers
If you need to use << or >> with 16 bit integers (which are not supported by the shift operators), typecasting will work.$0 $0 Example:$0 $0 short a = 2, b;    // 16 bit integer values (equivalent to System.Int16)       $0 b = a << 1; // Gives error "Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)$0 $0 b = (short)((int)a << 1); // Casting a to a 32 bit int, shifting, then casting the result back to a 16 bit int works.$0