Operator

&+(_:_:)

Returns the sum of the two given values, wrapping the result in case of any overflow.

Declaration

static func &+ (lhs: Int, rhs: Int) -> Int

Parameters

lhs

The first value to add.

rhs

The second value to add.

Discussion

The overflow addition operator (&+) discards any bits that overflow the fixed width of the integer type. In the following example, the sum of 100 and 121 is greater than the maximum representable Int8 value, so the result is the partial value after discarding the overflowing bits.

let x: Int8 = 10 &+ 21
// x == 31
let y: Int8 = 100 &+ 121
// y == -35 (after overflow)

For more about arithmetic with overflow operators, see Overflow Operators in The Swift Programming Language.

See Also

Masked Arithmetic

static func &- (Int, Int) -> Int

Returns the difference of the two given values, wrapping the result in case of any overflow.

static func &* (Int, Int) -> Int

Returns the product of the two given values, wrapping the result in case of any overflow.

static func &+= (inout Int, Int)

Adds two values and stores the result in the left-hand-side variable, wrapping any overflow.

static func &-= (inout Int, Int)

Subtracts the second value from the first and stores the difference in the left-hand-side variable, wrapping any overflow.

static func &*= (inout Int, Int)

Multiplies two values and stores the result in the left-hand-side variable, wrapping any overflow.