TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
>> operator is the right-shift operator in C#. It shifts the binary representation of its left-hand operand to the right by the number of bit positions specified by its right-hand operand.
Shift Mechanics: Arithmetic vs. Logical
The behavior of the>> operator depends strictly on the data type of the left-hand operand. C# distinguishes between arithmetic shifts (for signed types) and logical shifts (for unsigned types).
1. Arithmetic Right Shift (Signed Types)
When the left-hand operand is a signed integral type (int, long, nint), the operator performs an arithmetic shift. The empty high-order bit positions are populated with the value of the original sign bit (the most significant bit).
- If the number is positive (sign bit
0), it pads with0s. - If the number is negative (sign bit
1), it pads with1s.
uint, ulong, nuint), the operator performs a logical shift. The empty high-order bit positions are always populated with 0s, regardless of the original bit values.
Shift Count Masking
To prevent undefined behavior when the right-hand operand (count) exceeds the bit-width of the left-hand operand, C# applies a bitwise AND mask to the shift count.
- For 32-bit operands (
int,uint): The shift count is evaluated ascount & 0x1F. Only the lowest 5 bits of the count are used, restricting the shift to a range of 0–31. - For 64-bit operands (
long,ulong): The shift count is evaluated ascount & 0x3F. Only the lowest 6 bits of the count are used, restricting the shift to a range of 0–63. - For native-sized operands (
nint,nuint): The mask is either0x1For0x3Fdepending on whether the runtime architecture is 32-bit or 64-bit.
-1 applied to a 32-bit operand evaluates to -1 & 0x1F, which results in a shift of 31 bits.
Numeric Promotion
The>> operator is predefined for 32-bit (int, uint), 64-bit (long, ulong), and native-sized (nint, nuint) integers. If the left-hand operand is a narrower integral type (sbyte, byte, short, ushort), C# implicitly promotes it to an int before performing the shift operation. Consequently, the return type of the operation will be int.
Because byte and ushort are unsigned, they are zero-extended during promotion to int. The resulting int is guaranteed to be positive (sign bit 0). Therefore, the subsequent int >> int operation performs an arithmetic shift, but it pads with 0s because the promoted sign bit is 0.
Master C# with Deep Grasping Methodology!Learn More





