Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The >> 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.
result = expression >> count;

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 with 0s.
  • If the number is negative (sign bit 1), it pads with 1s.
// Positive signed integer (Sign bit is 0)
int a = 20;           // Binary: 00000000 00000000 00000000 00010100
int resultA = a >> 2; // Binary: 00000000 00000000 00000000 00000101 (Decimal: 5)

// Negative signed integer (Sign bit is 1)
int b = -20;          // Binary: 11111111 11111111 11111111 11101100
int resultB = b >> 2; // Binary: 11111111 11111111 11111111 11111011 (Decimal: -5)
2. Logical Right Shift (Unsigned Types) When the left-hand operand is an unsigned integral type (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.
// Unsigned integer (Always pads with 0)
uint c = 4294967276;    // Binary: 11111111 11111111 11111111 11101100
uint resultC = c >> 2;  // Binary: 00111111 11111111 11111111 11111011 (Decimal: 1073741819)

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 as count & 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 as count & 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 either 0x1F or 0x3F depending on whether the runtime architecture is 32-bit or 64-bit.
Because the operator strictly uses bitwise AND masking, negative shift counts wrap around using their two’s complement representation. For example, a shift count of -1 applied to a 32-bit operand evaluates to -1 & 0x1F, which results in a shift of 31 bits.
int x = 100;

// 33 & 0x1F evaluates to 1. The compiler executes this as x >> 1.
int result1 = x >> 33; 

// -1 & 0x1F evaluates to 31. The compiler executes this as x >> 31.
int result2 = x >> -1; 

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.
byte b = 16;

// byte result = b >> 2; // Compiler Error: Cannot implicitly convert type 'int' to 'byte'

int result = b >> 2;              // Correct: 'b' is promoted to int, result is int
byte castResult = (byte)(b >> 2); // Correct: Explicit cast back to byte
Master C# with Deep Grasping Methodology!Learn More