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

The `&` symbol in Swift serves three distinct syntactic and semantic roles depending on its context: as a binary bitwise AND operator for integer types, as a syntactic marker for passing references or pointers to functions, and as an infix operator for protocol composition in type signatures.

## 1. Bitwise AND Operator

When used as an infix operator between two integer types, `&` performs a bitwise AND operation. It evaluates the binary representation of both operands and returns a new integer of the same type. A bit in the resulting integer is set to `1` strictly if the corresponding bits in both the left-hand side (LHS) and right-hand side (RHS) operands are `1`. Otherwise, the bit is set to `0`.

**Syntax:**

```swift theme={"dark"}
let result = lhs & rhs
```

**Technical Characteristics:**

* **Associativity:** Left-associative.
* **Precedence:** Belongs to the `MultiplicationPrecedence` group. Unlike C-family languages where bitwise AND has lower precedence than equality operators, Swift's `&` evaluates before `ComparisonPrecedence` (e.g., `==`) and `AdditionPrecedence` (e.g., `|`, `^`). Consequently, an expression like `1 | 2 & 0` evaluates as `1 | (2 & 0)`, and `lhs & rhs == 0` evaluates as `(lhs & rhs) == 0`.
* **Type Constraints:** Both operands must conform to the `BinaryInteger` protocol and be of the exact same type.

**Execution:**

```swift theme={"dark"}
let lhs: UInt8 = 0b11001100 // Decimal: 204
let rhs: UInt8 = 0b10101010 // Decimal: 170

let result = lhs & rhs      // 0b10001000 (Decimal: 136)

// Precedence evaluation
let isZero = lhs & rhs == 0 // Evaluates as (lhs & rhs) == 0
```

## 2. In-Out Expression Marker

When used as a prefix at a function call site, `&` functions strictly as a syntactic marker (parsed grammatically as an `in-out-expression`), not as a prefix operator. It denotes that a variable is being passed as a reference or a pointer rather than by value. Because it is a marker and not an operator, it does not participate in operator precedence, cannot be overloaded, and Swift explicitly forbids defining `&` as a custom prefix operator.

**Syntax:**

```swift theme={"dark"}
functionName(&variable)
```

**Technical Characteristics:**

* **In-Out Parameters:** When bound to an `inout` parameter, `&` invokes Swift's "copy-in copy-out" (call-by-value-result) memory model. The value is copied upon invocation, mutated locally, and copied back to the original memory location upon return.
* **Implicit Pointer Conversion:** When passed to a function expecting an `UnsafePointer<T>` or `UnsafeMutablePointer<T>`, `&` yields the memory address of the variable. Passing via `&` does *not* inherently grant mutation rights; if the parameter is `UnsafePointer<T>`, the memory is strictly immutable.
* **Mutability Constraints:** The operand prefixed by `&` must be a mutable variable (declared with `var`), regardless of whether the receiving function mutates the memory or reads it immutably via an `UnsafePointer`. It cannot be a constant (`let`), a literal, or a computed property lacking a setter.
* **Exclusivity:** Swift's Law of Exclusivity dictates that passing a variable via `&` requires exclusive access to that variable's memory for the duration of the function call, preventing concurrent access traps.

**Execution:**

```swift theme={"dark"}
// 1. Binding to an inout parameter (mutation permitted)
func increment(value: inout Int) { value += 1 }
var counter = 0
increment(value: &counter)

// 2. Binding to an UnsafePointer (strictly immutable)
func readPointer(_ ptr: UnsafePointer<Int>) { /* ... */ }
var readOnlyTarget = 42
readPointer(&readOnlyTarget) 
```

## 3. Protocol Composition Operator

When used as an infix operator within a type signature, `&` performs protocol composition. It combines multiple protocols (and optionally up to one class) into a single requirement, representing an existential type or a generic constraint that must conform to all specified types simultaneously.

**Syntax:**

```swift theme={"dark"}
TypeA & TypeB
```

**Technical Characteristics:**

* **Type Intersection:** The operator does not create a new concrete type at runtime. Instead, it creates a compile-time intersection of type requirements.
* **Constraints:** The composition can include any number of protocols but is restricted to a maximum of one class type.
* **Contexts:** It is strictly a type-level operator used in variable declarations, function parameters, generic `where` clauses, and `typealias` definitions.

**Execution:**

```swift theme={"dark"}
protocol Renderable { /* ... */ }
protocol Animatable { /* ... */ }

// Typealias composition
typealias InteractiveNode = Renderable & Animatable

// Generic constraint composition using standard library protocols
func process<T: Sequence & Identifiable>(item: T) { /* ... */ }

// Existential type composition in a variable declaration
var handler: any Hashable & Sendable
```

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