> ## 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 `>>=` (bitwise right shift assignment) operator is a compound assignment operator that shifts the binary representation of its evaluated left operand to the right by the number of bit positions specified by its right operand, and subsequently assigns the resulting value back to the left operand.

## Syntax

```c theme={"dark"}
left_operand >>= right_operand;
```

This operation is semantically equivalent to:

```c theme={"dark"}
left_operand = left_operand >> right_operand;
```

*Note: The critical distinction is that in the compound assignment (`>>=`), the `left_operand` is evaluated exactly once. This is strictly enforced to prevent multiple evaluations of side effects (e.g., `arr[i++] >>= 2;`).*

## Technical Mechanics

**Operand Constraints and Result Type:**
Both operands must be of integer types. Before the shift operation occurs, both operands undergo standard integer promotions. However, unlike the standalone `>>` operator (where the result type is the promoted type of the left operand), the type of the resulting `>>=` assignment expression is the **unqualified type of the left operand** (its original type prior to integer promotion). For example, if `c` is a `uint8_t`, the expression `c >>= 2` yields a `uint8_t`, whereas `c >> 2` yields an `int`.

**Integer Promotion and Bit Filling Behavior:**
The bits vacated at the most significant bit (MSB) positions are filled based on the type and value of the left operand *after* integer promotion:

1. **Promoted Unsigned Types:** If the promoted left operand has an unsigned type, a logical right shift is performed. The vacated MSBs are strictly filled with `0`s.
2. **Promoted Signed Types (Non-negative):** If the promoted left operand has a signed type and a positive value (or zero), the vacated MSBs are filled with `0`s.

   *Crucially, narrow unsigned types (such as `uint8_t` or `unsigned short`) are typically promoted to signed `int`. Because their promoted values are guaranteed to be positive, the arithmetic shift fills the vacated bits with `0`s, effectively mirroring the behavior of a logical shift.*
3. **Promoted Signed Types (Negative):** If the promoted left operand has a signed type and a negative value, the behavior is implementation-defined. Virtually all modern C compilers implement this as an arithmetic right shift (sign extension), where the vacated MSBs are filled with `1`s to preserve the negative sign.

## Undefined Behavior (UB)

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

* The `right_operand` is strictly negative.
* The `right_operand` is greater than or equal to the width (in bits) of the **promoted** `left_operand`. For example, shifting a 16-bit `short` right by 16 positions is valid if it promotes to a 32-bit `int`, but shifting a 32-bit `int` right by 32 or more positions is UB.

## Execution Example

```c theme={"dark"}
#include <stdio.h>
#include <stdint.h>

int main() {
    // 1. Narrow unsigned operation (Promoted to signed int, positive value)
    uint8_t u_val = 200;      // Binary: 11001000
    u_val >>= 2;              // Promoted to int: 200 >> 2 = 50.
                              // Cast back to uint8_t. Result: 00110010
                              // Expression type remains uint8_t.

    // 2. Signed operation (Implementation-defined, typically Sign Extension)
    int8_t s_val = -56;       // Binary: 11001000 (Two's complement)
    s_val >>= 2;              // Promoted to int: -56 >> 2 = -14.
                              // Cast back to int8_t. Result: 11110010
                              // Vacated MSBs filled with 1.

    // 3. Evaluation of side effects
    int arr[] = {16, 32, 64};
    int i = 1;
    arr[i++] >>= 2;           // arr[1] becomes 8. 'i' is evaluated and incremented exactly once.
    
    return 0;
}
```

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