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

The `>>=` (right shift assignment) operator is a compound assignment operator that performs a bitwise right shift on its left operand by the number of bit positions specified by its right operand, subsequently assigning the computed result back to the left operand.

```cpp theme={"dark"}
lhs >>= rhs;
```

Semantically, the expression `lhs >>= rhs` is equivalent to `lhs = lhs >> rhs`, with the strict guarantee that the expression `lhs` is evaluated exactly once.

## Operand Constraints

The operands have distinct type requirements:

* **Left Operand (`lhs`):** Must be a modifiable lvalue of an **integral type** (e.g., `char`, `short`, `int`, `long`, `unsigned`). It cannot be an enumeration type, because C++ does not allow implicit conversions from the integer result of the shift operation back to an enumeration.
* **Right Operand (`rhs`):** Must be of an **integral type** or an **unscoped enumeration type** (which implicitly promotes to an integer).

While the underlying bitwise shift (`>>`) applies standard integer promotions to the operands before the shift operation occurs, the compound assignment operator converts the computed result back to the original type of `lhs`. The final result of the expression is an lvalue reference to the *unpromoted* left operand.

## Bit-Level Behavior

The treatment of the vacated Most Significant Bits (MSBs) depends strictly on the signedness of the left operand and the C++ standard version:

* **Unsigned Integers:** The operator performs a **logical right shift**. The vacated MSBs are unconditionally filled with zeros.
* **Signed Integers (Non-negative values):** The operator performs a logical right shift, filling vacated MSBs with zeros.
* **Signed Integers (Negative values):**
  * **C++20 and later:** The operator is guaranteed to perform an **arithmetic right shift**. It utilizes sign-extension, filling the vacated MSBs with `1`s (the original sign bit). This is mathematically defined as division by $2^{rhs}$ rounding towards negative infinity.
  * **Pre-C++20:** The behavior of right-shifting a negative signed integer is implementation-defined (though the vast majority of compilers historically implemented sign-extension).

## Undefined Behavior (UB)

The `>>=` operator invokes undefined behavior under the following conditions:

1. **Negative Shift Count:** The right operand (`rhs`) is less than `0`.
2. **Oversized Shift Count:** The right operand (`rhs`) is greater than or equal to the total number of bits in the *promoted* left operand. For example, if `lhs` promotes to a 32-bit integer, `lhs >>= 32` and `lhs >>= 33` result in undefined behavior.

## Return Value

The operator returns an lvalue reference to the modified left operand (`lhs`), allowing for operator chaining (e.g., `a >>= b >>= c`). Such chaining evaluates right-to-left due to the operator's right-associativity.

## Overloading

For user-defined types (classes or structs), the `>>=` operator can be overloaded by defining a function named `operator>>=`. When overloaded, the operator evaluates its operands exactly once as function arguments, preserving the standard single-evaluation guarantee.

```cpp theme={"dark"}
class T {
public:
    T& operator>>=(const T2& rhs); // Typical signature for member overload
};
```

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