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 multiplication operator. It performs integer multiplication while explicitly allowing the mathematical result to exceed the bit-width capacity of the integer type. Instead of triggering a runtime trap (which is the default behavior of the standard * operator), &* silently truncates the result by discarding the most significant bits that do not fit within the type’s memory footprint.

Syntax

let result = lhs &* rhs
  • lhs: The left-hand side integer operand.
  • rhs: The right-hand side integer operand.
  • result: The product of the two operands, constrained to the exact same integer type as the operands.

Mechanics and Behavior

Swift integers are strictly bounded by their bit width. For example, an 8-bit unsigned integer (UInt8) can only represent values from 0 to 255. When a multiplication operation produces a value outside this representable range, an overflow occurs.
  1. Standard Multiplication (*): Evaluates the product and checks if it fits within the type’s bounds. If it overflows, the Swift runtime intentionally traps, crashing the program to guarantee memory safety and prevent undefined behavior.
  2. Overflow Multiplication (&*): Bypasses the bounds check. It calculates the product using standard binary arithmetic and applies a modulo operation based on the type’s bit width (2n2^n, where nn is the number of bits). The extra high-order bits are discarded, and the remaining bits are interpreted according to the type’s encoding (unsigned or two’s complement for signed integers).

Unsigned Integer Overflow

When using &* with unsigned integers, the result wraps around starting from zero. The operation is mathematically equivalent to (lhs * rhs) % (maximum_value + 1).
let a: UInt8 = 100
let b: UInt8 = 3

// Mathematical product: 300
// Binary representation of 300: 100101100 (9 bits)
// Truncated to 8 bits:           00101100 (Decimal 44)
let result = a &* b 
print(result) // Prints: 44

Signed Integer Overflow

When using &* with signed integers, the operation still discards the overflowing high-order bits, but the remaining bits are interpreted using two’s complement representation. Because the sign bit (the most significant bit) can be overwritten during truncation, multiplying two positive numbers can result in a negative number, and vice versa.
let x: Int8 = 100
let y: Int8 = 2

// Mathematical product: 200
// Binary representation of 200: 00000000 11001000 (16 bits)
// Truncated to 8 bits:                   11001000
// In two's complement, 11001000 represents -56
let signedResult = x &* y
print(signedResult) // Prints: -56

Type Constraints

The &* operator requires both operands to be of the exact same integer type. It cannot be used to multiply a UInt8 by an Int, nor can it be used with floating-point types (Float, Double), as floating-point numbers handle overflow differently (typically yielding Infinity rather than wrapping at the bit level).
Master Swift with Deep Grasping Methodology!Learn More