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

The `&<<` operator is the **masking bitwise left shift operator** in Swift. It shifts the binary representation of an integer to the left by a specified number of bits. Unlike the standard bitwise left shift operator (`<<`), which triggers a runtime trap on negative shift amounts and returns `0` when the shift amount is greater than or equal to the type's bit width, `&<<` masks the shift amount. This guarantees a valid operation without runtime traps and causes overshifts to wrap cyclically.

## Syntax

```swift theme={"dark"}
let result = value &<< shiftAmount
```

## Masking Mechanics

When evaluating `lhs &<< rhs`, Swift does not directly shift `lhs` by `rhs`. Instead, it calculates an effective shift amount by performing a bitwise AND operation between the shift amount (`rhs`) and one less than the bit width of the value being shifted (`lhs.bitWidth - 1`).

Because Swift's integer types always have bit widths that are powers of two (8, 16, 32, 64), this masking operation effectively constrains the shift amount to the strict range of `0..<(lhs.bitWidth)`.

**Formula:**
`Effective Shift = rhs & (lhs.bitWidth - 1)`

## Evaluation Examples

### 1. Standard Shift (Shift amount \< Bit Width)

When the shift amount is positive and strictly less than the bit width, `&<<` behaves identically to `<<`.

```swift theme={"dark"}
let base: UInt8 = 0b0000_0001 // Bit width is 8
let shift: UInt8 = 2

// Effective shift: 2 & (8 - 1) -> 2 & 7 -> 2
let result = base &<< shift 
// result == 0b0000_0100 (4)
```

### 2. Overshift (Shift amount >= Bit Width)

When the shift amount exceeds the bit width, the standard `<<` operator performs a "smart shift" and evaluates to `0`. The `&<<` operator instead masks the value, effectively wrapping the shift amount.

```swift theme={"dark"}
let base: UInt8 = 0b0000_0001 // Bit width is 8
let overShift: UInt8 = 10

// Effective shift: 10 & (8 - 1) -> 10 & 7 -> 2
let result = base &<< overShift 
// result == 0b0000_0100 (4)
```

### 3. Exact Bit Width Shift

Shifting by the exact bit width results in an effective shift of zero, leaving the original value unchanged.

```swift theme={"dark"}
let base: UInt8 = 0b0000_0011
let exactShift: UInt8 = 8

// Effective shift: 8 & (8 - 1) -> 8 & 7 -> 0
let result = base &<< exactShift 
// result == 0b0000_0011 (3)
```

### 4. Negative Shift Amounts

If the right-hand side (`rhs`) is a signed integer and evaluates to a negative number, the standard `<<` operator triggers a fatal error. The `&<<` operator prevents this by masking the two's complement binary representation of that negative number against `bitWidth - 1`. It does *not* reverse the direction to a right shift.

```swift theme={"dark"}
let base: UInt8 = 0b0000_0001
let negativeShift: Int8 = -2 // Two's complement: 1111_1110

// Effective shift: -2 & 7 -> 6
let result = base &<< negativeShift
// result == 0b0100_0000 (64)
```

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