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

# Swift Bitwise Right Shift Assignment

The `>>=` (bitwise right shift assignment) operator shifts the binary representation of a mutable integer variable to the right by a specified number of bits and assigns the resulting value back to that same variable. It is a compound assignment operator that combines the bitwise right shift (`>>`) and assignment (`=`) operations into a single expression.

## Syntax

```swift theme={"dark"}
lhs >>= rhs
```

* **`lhs`** (Left-Hand Side): A mutable variable (`var`) of an integer type conforming to the `BinaryInteger` protocol.
* **`rhs`** (Right-Hand Side): The scalar number of bit positions to shift `lhs` to the right. Must also conform to `BinaryInteger`.

The expression `a >>= b` is semantically identical to `a = a >> b`.

## Bitwise Mechanics

The behavior of the `>>=` operator depends strictly on whether the underlying integer type of `lhs` is unsigned or signed. Bits that are shifted past the rightmost boundary (the least significant bit) are permanently discarded.

### 1. Unsigned Integers (Logical Right Shift)

When applied to an unsigned integer (e.g., `UInt8`, `UInt32`), `>>=` performs a **logical right shift**. As bits are shifted to the right, the newly vacated bit positions on the left (the most significant bits) are padded with zeros.

```swift theme={"dark"}
var unsignedByte: UInt8 = 0b11110000 // Decimal: 240
unsignedByte >>= 2

// Execution:
// 1. Shift right 2:  --111100
// 2. Pad with zeros: 00111100
// unsignedByte is now 0b00111100 (Decimal: 60)
```

### 2. Signed Integers (Arithmetic Right Shift)

When applied to a signed integer (e.g., `Int8`, `Int`), `>>=` performs an **arithmetic right shift**. To preserve the integer's sign in Two's Complement representation, the newly vacated bit positions on the left are padded with the original **sign bit** (the most significant bit prior to the shift).

* If the number is positive (sign bit is `0`), it pads with `0`s.
* If the number is negative (sign bit is `1`), it pads with `1`s.

```swift theme={"dark"}
// Positive Signed Integer
var positiveByte: Int8 = 0b01110000 // Decimal: 112 (Sign bit is 0)
positiveByte >>= 2

// Execution:
// 1. Shift right 2:  --011100
// 2. Pad with sign bit (0): 00011100
// positiveByte is now 0b00011100 (Decimal: 28)

// Negative Signed Integer
var negativeByte: Int8 = Int8(bitPattern: 0b11110000) // Decimal: -16 (Sign bit is 1)
negativeByte >>= 2

// Execution:
// 1. Shift right 2:  --111100
// 2. Pad with sign bit (1): 11111100
// negativeByte is now 0b11111100 (Decimal: -4)
```

## Type Constraints and Safety

* **Type Matching:** Both `lhs` and `rhs` must conform to the `BinaryInteger` protocol. They do not need to be the exact same type (e.g., shifting an `Int8` by an `Int` is valid).
* **Negative Shift Amounts (Runtime Trap):** If `rhs` is a signed integer type and evaluates to a negative value, Swift will trigger a runtime crash (fatal error). The shift amount must always be greater than or equal to zero.
* **Overshifting:** If `rhs` is greater than or equal to the bit width of `lhs` (e.g., shifting an 8-bit integer by 10), Swift does not invoke undefined behavior. Instead, it shifts all original bits out of bounds. For unsigned or positive signed integers, the result becomes `0`. For negative signed integers, the result becomes `-1` (all bits set to `1`).

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