Skip to main content

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.

The &+ operator is Swift’s overflow addition operator. It performs integer addition while explicitly allowing the result to exceed the memory bounds of the underlying integer type, silently wrapping around the type’s limits instead of triggering a runtime trap. In Swift, standard arithmetic operators (like +) are safe by default; if an operation results in a value outside the representable range of the type, the program halts with an overflow error. The &+ operator bypasses this safety check, utilizing two’s complement arithmetic to truncate the bits that exceed the available bit width.

Syntax

let result = leftOperand &+ rightOperand
Note: Both operands must be of the exact same integer type.

Overflow Behavior

The wrapping behavior differs mechanically depending on whether the integer type is unsigned or signed.

Unsigned Integers

When an unsigned integer exceeds its maximum representable value, it wraps around to zero and continues counting upward.
let maxUnsigned: UInt8 = 255 // Binary: 11111111
let wrappedUnsigned = maxUnsigned &+ 1
// Result: 0 (Binary: 00000000)
Mechanism: 255 + 1 requires 9 bits (100000000). The &+ operator discards the 9th bit (the overflow bit), leaving the 8-bit representation of 0.

Signed Integers

When a signed integer exceeds its maximum representable value, the overflow alters the sign bit, causing the value to wrap around to the type’s minimum representable (negative) value.
let maxSigned: Int8 = 127 // Binary: 01111111
let wrappedSigned = maxSigned &+ 1
// Result: -128 (Binary: 10000000)
Mechanism: 127 + 1 results in 10000000. In an 8-bit signed integer using two’s complement, the most significant bit is the sign bit. Setting it to 1 changes the value to -128.

Compound Assignment

The overflow addition operator can also be used as a compound assignment operator (&+=), which mutates the left-hand operand in place.
var counter: UInt8 = 255
counter &+= 1 
// counter is now 0
The &+ operator is part of a specific family of overflow operators in Swift designed to handle bitwise truncation, which also includes:
  • &- (Overflow Subtraction)
  • &* (Overflow Multiplication)
Master Swift with Deep Grasping Methodology!Learn More