>> Operator (C# Reference)

Switch View :
ScriptFree
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

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


See Also

Concepts

Reference

Other Resources

Community Content

Frank Migacz
What is meant by -
A 32-bit number can only be shifted 32 digits, and the number of shifted digits can be represented by 5 binary bits (2 ^5 = 32).
Likewise, a 64-bit number can only be shifted 64 digits, allowing only 6 bits to specify how far to shift the number.

The physical representation of a signed (positive or negative) integer includes bits that do not only describe the number, but its sign. The manual here states that the sign will not change by shifting a signed integer.

For an unsigned integer, there is no representation of sign, so any bit in the integer that is unused (such as the first 25 bits of the number 127 - 00000000 00000000 00000000 01111111) will be zeroes. In other words, the sign of an unsigned integer will not change, either.

SUMMARY: do not attempt to shift a 32-bit number more than 32 places. Do not attempt to shift a 64-bit number more than 64 places. Bit-wise shifting results are of the same sign (positive / negative) as the original number. Always.

FOLLOW-UP: research "two's complement"

lpbcorp
What do you mean by -
low-order five bits ?
low-order six bits ?
high-order empty bits ?
the sign bit?