C# Language Reference
>> 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

// cs_operator_right_shift.cs
using System;
class MainClass
{
    static void Main() 
    {
        int i = -1000;
        Console.WriteLine(i >> 3);
    }
}

Output

-125
See Also

Reference

C# Operators

Concepts

C# Programming Guide

Other Resources

C# Reference

Tags :


Community Content

MIKEY MIGEE
Lower level look at this
To explain a bit more in depth:

1000 = 1111101000

Shift 3

1111101000 becomes
0001111101 (or simply 1111101)

and 1111101 converted back to decimal will get 125

Tags :

ynamara
Why mask the second operand?
Is there a good reason why the second operand (shift count) is masked? I was completely thrown off today when I found out that 1L >> 64 == 1.
Tags :

Page view tracker