> ## 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 Masking Left Shift Assignment

The `&<<=` (masking bitwise left shift assignment) operator first masks a requested shift amount to ensure it remains within the bit-width limits of the data type, shifts the bit pattern of a mutable integer variable to the left by that masked amount, and then assigns the resulting value back to the original variable.

```swift theme={"dark"}
lhs &<<= rhs
```

## Technical Mechanics

* **`lhs` (Left-Hand Side):** A mutable variable of a type that conforms to the `FixedWidthInteger` protocol.
* **`rhs` (Right-Hand Side):** The requested number of bit positions to shift.
* **Masking (`&`):** The operator automatically applies a bitmask to the `rhs` *before* the shift occurs, constraining the shift amount to the bit width of the `lhs`.
* **Assignment (`=`):** This is a compound assignment operator; it mutates the `lhs` in place and evaluates to `Void` (`()`).

## Masking Calculation

The actual shift amount applied to the `lhs` is determined by a bitwise AND operation between the `rhs` and the bit width of the `lhs` minus one:

`actual_shift = rhs & (lhs.bitWidth - 1)`

For example, if `lhs` is an `UInt8` (which has a bit width of 8), the mask applied to the shift amount is `7` (binary `00000111`). If you attempt to shift an `UInt8` by `10` positions, the operator masks the `10` (`00001010`) with `7` (`00000111`), resulting in an actual shift of `2` positions.

## Syntax Visualization

```swift theme={"dark"}
var bitPattern: UInt8 = 0b0000_0011 // Decimal 3

// Attempting to shift by 10.
// Masking calculation: 10 & (8 - 1) = 10 & 7 = 2.
// The actual operation performed is a left shift by 2.
bitPattern &<<= 10 

// 0b0000_0011 shifted left by 2 becomes 0b0000_1100 (Decimal 12)
print(bitPattern) // Prints 12

var negativeShiftPattern: UInt8 = 0b0000_0001 // Decimal 1

// Attempting to shift by -1.
// Masking calculation: -1 (11111111 in 8-bit two's complement) & 7 = 7.
// The actual operation performed is a left shift by 7.
negativeShiftPattern &<<= -1

// 0b0000_0001 shifted left by 7 becomes 0b1000_0000 (Decimal 128)
print(negativeShiftPattern) // Prints 128
```

*Note:* In Swift, the standard left shift assignment operator (`<<=`) performs a "smart shift." If the shift amount is greater than or equal to the type's bit width, `<<=` assigns `0` to the variable. Additionally, the standard shift operator triggers a runtime error (trap) if the shift amount is *negative*. The `&<<=` operator exists specifically to provide a wrapping masking behavior for the shift amount. This bypasses the zeroing effect when the requested shift exceeds the type's bit width, and it prevents the negative shift trap by safely masking the two's complement representation of a negative shift amount into a valid positive shift.

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