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

The `<<` (bitwise left shift) operator shifts all bits in an integer's binary representation to the left by a specified number of positions. In Swift, `<<` implements "smart shift" semantics, safely handling over-shifting and negative shift amounts without triggering runtime traps or undefined behavior.

```swift theme={"dark"}
let result = leftOperand << rightOperand
```

## Technical Mechanics

When the `<<` operator is applied, it performs the following bit-level operations:

1. **Displacement:** Every bit in the `leftOperand` is moved to the left by the number of positions defined by the `rightOperand`.
2. **Zero Extension:** The vacated bit positions on the least significant bit (LSB) side (the right side) are filled with zeros.
3. **Truncation:** Any bits shifted beyond the bounds of the most significant bit (MSB) for the integer's specific type width (e.g., beyond the 8th bit in a `UInt8`) are permanently discarded.

## Smart Shift Semantics

Unlike languages that strictly enforce shift bounds or exhibit undefined behavior on out-of-bounds shifts, Swift's `<<` operator safely evaluates any integer passed as the `rightOperand`:

* **Over-shifting:** If the shift amount is greater than or equal to the bit width of the `leftOperand`, the operator evaluates to `0`. All original bits are safely shifted out of bounds.
* **Negative Shifting:** If the shift amount is less than `0`, the operator reverses direction. A negative left shift safely performs a right shift (`>>`) by the absolute value of the shift amount.

*Note: This behavior distinguishes `<<` from Swift's masking left shift operator (`&<<`), which wraps the shift amount using modulo arithmetic based on the type's bit width rather than evaluating to `0` or reversing direction.*

## Syntax and Type Constraints

```swift theme={"dark"}
static func << <RHS: BinaryInteger>(lhs: Self, rhs: RHS) -> Self
```

* **Return Type:** The resulting value retains the exact type of the `leftOperand` (`Self`).
* **Protocol Conformance:** Both operands must conform to the `BinaryInteger` protocol. However, they are not required to be the exact same type. The right-hand operand (`RHS`) can be any type conforming to `BinaryInteger`, allowing you to shift an `Int64` by an `Int8`, for example.

## Bitwise Visualization

The following demonstrates the mechanical shift of an 8-bit unsigned integer:

```swift theme={"dark"}
let initialValue: UInt8 = 0b0001_0111 
let shiftedValue = initialValue << 3   

// Bit-level transformation:
// [0 0 0 1 0 1 1 1] (initialValue)
//  \ \ \ \ \ \ \ \
//   \ \ \ \ \ \ \ \
// [1 0 1 1 1 0 0 0] (shiftedValue)
```

The following demonstrates Swift's smart shift handling of out-of-bounds and negative values:

```swift theme={"dark"}
let value: UInt8 = 0b1111_1111

// Over-shifting (>= 8 bits for UInt8) evaluates to 0
let overShifted = value << 10 
// Result: 0b0000_0000

// Negative shifting reverses direction (acts as >> 2)
let negativeShifted = value << -2 
// Result: 0b0011_1111
```

## Signed Integer Behavior

The `<<` operator manipulates the raw bit pattern regardless of whether the integer type is signed or unsigned. When applied to signed integers (e.g., `Int8`), the shift does not preserve the sign bit. If a `1` is shifted into the MSB (the sign bit in two's complement representation), the logical value of the integer will invert from positive to negative.

```swift theme={"dark"}
let signedValue: Int8 = 0b0011_0000 // Positive 48
let shiftedSigned = signedValue << 2

// Bit-level transformation:
// [0 0 1 1 0 0 0 0] (signedValue)
// [1 1 0 0 0 0 0 0] (shiftedSigned - MSB is now 1, resulting in Negative 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>
