> ## 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.

# C# Right Shift

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.

```csharp theme={"dark"}
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 `0`s.
* If the number is negative (sign bit `1`), it pads with `1`s.

```csharp theme={"dark"}
// 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 `0`s, regardless of the original bit values.

```csharp theme={"dark"}
// 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.

```csharp theme={"dark"}
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 `0`s because the promoted sign bit is `0`.

```csharp theme={"dark"}
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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
