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

# Rust Right Shift

The `>>` operator is the bitwise right shift operator. It shifts the binary representation of the left-hand operand to the right by the number of bit positions specified by the right-hand operand.

```rust theme={"dark"}
let result = left_operand >> right_operand;
```

## Shift Mechanics: Logical vs. Arithmetic

The behavior of the `>>` operator depends strictly on the type of the left-hand operand:

* **Unsigned Integers (`u8`, `u16`, `u32`, `u64`, `u128`, `usize`)**: Performs a **logical right shift**. The bits are shifted right, and the vacated most significant bits (MSBs) on the left are filled with zeros (`0`).
* **Signed Integers (`i8`, `i16`, `i32`, `i64`, `i128`, `isize`)**: Performs an **arithmetic right shift**. The bits are shifted right, and the vacated MSBs are filled with the original sign bit (sign extension). This preserves the sign of the two's complement negative number.

```rust theme={"dark"}
// Logical right shift (Unsigned)
let unsigned_val: u8 = 0b1111_0000; // 240
let logical_shift = unsigned_val >> 2; 
// Result: 0b0011_1100 (60) - Vacated bits filled with 0

// Arithmetic right shift (Signed)
// Note: Explicit cast is required as 0b1111_0000 exceeds i8::MAX
let signed_val = 0b1111_0000_u8 as i8; // -16 (Two's complement)
let arithmetic_shift = signed_val >> 2; 
// Result: 0b1111_1100_u8 as i8 (-4) - Vacated bits filled with 1 (sign bit)
```

## Trait Implementation

The `>>` operator is syntactic sugar for the `std::ops::Shr` trait. The compound assignment operator `>>=` is backed by the `std::ops::ShrAssign` trait.

```rust theme={"dark"}
pub trait Shr<Rhs = Self> {
    type Output;
    fn shr(self, rhs: Rhs) -> Self::Output;
}
```

In Rust, the right-hand operand (`Rhs`) does not need to be the same type as the left-hand operand. The standard library implements `Shr` for all combinations of integer types (e.g., shifting a `u64` by a `u8` is perfectly valid).

## Overflow and Panic Behavior

The `>>` operator enforces strict rules regarding the validity of the right-hand operand to prevent undefined behavior:

* **Negative Shift Amounts**: Because `Shr` accepts signed integers for the right-hand operand, passing a negative value (e.g., `val >> -1_i32`) is syntactically valid but will unconditionally panic at runtime in **both** Debug and Release modes.
* **Compile-time Out-of-Bounds**: Hardcoding a shift amount greater than or equal to the total number of bits in the left operand's type (e.g., shifting a `u32` by `32`) triggers an `arithmetic_overflow` compiler error.
* **Runtime Out-of-Bounds (Debug mode)**: Shifting by a value greater than or equal to the bit-width of the left operand triggers a panic.
* **Runtime Out-of-Bounds (Release mode)**: Performs a masked shift. The right operand is masked to the bit-width of the left operand minus one (e.g., `val >> (shift & 31)` for a `u32`).

## Explicit Shift Methods

To bypass default operator behavior and explicitly control out-of-bounds shifts, Rust provides explicit methods on integer primitives. Unlike the `>>` operator, these methods strictly require a `u32` for the right-hand operand. Attempting to pass a negative value to these methods results in a compile-time type mismatch error rather than a runtime panic.

* `checked_shr(n: u32)`: Returns `None` if `n` is greater than or equal to the bit-width of the left operand.
* `wrapping_shr(n: u32)`: Explicitly performs the masked shift regardless of the compilation profile.
* `overflowing_shr(n: u32)`: Returns a tuple `(result, bool)` where the boolean indicates if the shift amount `n` was out of bounds.

<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 Rust 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>
