Arithmetic and Logical Operators
Arithmetic operators
The unary pefix operator -
negates an integer:
_10let a = 1_10-a // is `-1`
There are four binary arithmetic operators:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Remainder:
%
_10let a = 1 + 2_10// `a` is `3`
The arguments for the operators need to be of the same type. The result is always the same type as the arguments.
The division and remainder operators abort the program when the divisor is zero.
Arithmetic operations on the signed integer types Int8
, Int16
, Int32
, Int64
, Int128
, Int256
, and on the unsigned integer types UInt8
, UInt16
, UInt32
, UInt64
, UInt128
, and UInt256
do not cause values to overflow or underflow - the program will abort with a fatal overflow error.
_10let a: UInt8 = 255_10_10// Run-time error: The result `256` does not fit in the range of `UInt8`,_10// thus a fatal overflow error is raised and the program aborts_10//_10let b = a + 1
_10let a: Int8 = 100_10let b: Int8 = 100_10_10// Run-time error: The result `10000` does not fit in the range of `Int8`,_10// thus a fatal overflow error is raised and the program aborts_10//_10let c = a * b
_10let a: Int8 = -128_10_10// Run-time error: The result `128` does not fit in the range of `Int8`,_10// thus a fatal overflow error is raised and the program aborts_10//_10let b = -a
Arithmetic operations on the unsigned integer types Word8
, Word16
, Word32
, and Word64
can cause values to overflow or underflow.
For example, the maximum value of an unsigned 8-bit integer is 255 (binary 11111111). Adding 1 results in an overflow, truncation to 8 bits, and the value 0:
_10// 11111111 = 255_10// + 1_10// = 100000000 = 0
_10let a: Word8 = 255_10a + 1 // is `0`
Similarly, for the minimum value 0, subtracting 1 wraps around and results in the maximum value 255:
_10// 00000000_10// - 1_10// = 11111111 = 255
_10let b: Word8 = 0_10b - 1 // is `255`
Arithmetics on number super-types
Arithmetic operators are not supported for number supertypes (Number
, SignedNumber
, FixedPoint
, SignedFixedPoint
, Integer
, and SignedInteger
), as they may or may not succeed at run-time:
_10let x: Integer = 3 as Int8_10let y: Integer = 4 as Int8_10_10let z: Integer = x + y // Static error
Values of these types must be cast to the desired type before performing the arithmetic operation:
_10let z: Integer = (x as! Int8) + (y as! Int8)
Logical operators
Logical operators work with the boolean values true
and false
.
-
Logical NOT:
!a
This unary prefix operator logically negates a boolean:
_10let a = true_10!a // is `false` -
Logical AND:
a && b
_10true && true // is `true`_10_10true && false // is `false`_10_10false && true // is `false`_10_10false && false // is `false`If the left-hand side is false, the right-hand side is not evaluated.
-
Logical OR:
a || b
_10true || true // is `true`_10_10true || false // is `true`_10_10false || true // is `true`_10_10false || false // is `false`If the left-hand side is true, the right-hand side is not evaluated.