- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift - Arithmetic Overflow Operators
Arithmetic overflow operators are the special type of operators that are used to perform arithmetic operations on integer types and handle overflow very efficiently. They are generally used when we want to perform calculations that may exceed the maximum or minimum bound of the given integer.
For example, we are adding two integers of Int8 type which exceed the maximum bound of the Int8(-128 to 127). So if we are using the normal addition operator "+" we will get an error because we are not allowed to store numbers greater than the maximum or minimum bound in Int8 but using arithmetic overflow operators we can easily overflow the minimum and maximum bound of Int8 without any error.
Swift supports three types of arithmetic overflow operators −
Operator | Name | Example |
---|---|---|
&+ | Overflow Addition | X &+ Y |
&- | Overflow Subtraction | X &- Y |
&* | Overflow Multiplication | X &* Y |
Overflow Value
Before understanding the workings of the overflow operators, we first understand what overflow value is. As we know an integer has a particular range with minimum and maximum values such as Int8 can store values from -128 to 127.
So the overflow value represents the value that exceeds the minimum or maximum range of the given integer. This generally happens when we work with overflow arithmetic operators.
We can overflow signed and unsigned integers. When we overflow in a positive direction it will wrap around from the maximum valid integer value to the minimum whereas when we overflow in a negative direction it will wrap around from the minimum valid integer value to the maximum.
Example
var num = UInt8.max num = num &+ 1
Here an unsigned integer is flowing in the positive direction with the help of the &+ operator because the maximum value of UInt8 is 255 or the binary representation is 11111111.
Now we add 1 to num which will exceed its maximum value. So this overflow is handled by wrapping around the minimum representable value of UInt8 which is 0.
Overflow Addition "&+" in Swift
The overflow Addition "&+" operator is used to add two integers with overflow checking. If any overflow occurs, then it will handle it properly without causing an error to the program. This operator can work with both signed and unsigned integers.
Syntax
Following is the syntax of the overflow addition operator −
var value = x &+ b
Example
Swift program to calculate the sum of two integers of Int8 types.
import Foundation // Defining integer data type let num1 : Int8 = 110 let num2 : Int8 = 30 // Calculate the sum using the normal addition operator var sum = num1 + num2 print("Sum of \(num1) and \(num2) = \(sum)")
Output
Stack dump: 0. Program arguments: /opt/swift/bin/swift-frontend -frontend -interpret main.swift -disable-objc-interop -color-diagnostics -new-driver-path /opt/swift/bin/swift-driver -empty-abi-descriptor -resource-dir /opt/swift/lib/swift -module-name main 1. Swift version 5.7.3 (swift-5.7.3-RELEASE) 2. Compiling with the current language version 3. While running user code "main.swift" Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it): /opt/swift/bin/swift-frontend(+0x551a103)[0x55731fee8103] /opt/swift/bin/swift-frontend(+0x551802e)[0x55731fee602e] /opt/swift/bin/swift-frontend(+0x551a48a)[0x55731fee848a] /lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f138af33520] [0x7f1389bea35f] /opt/swift/bin/swift-frontend(+0x1940b3d)[0x55731c30eb3d] /opt/swift/bin/swift-frontend(+0xc74db9)[0x55731b642db9] /opt/swift/bin/swift-frontend(+0xa454c6)[0x55731b4134c6] /opt/swift/bin/swift-frontend(+0xa419b6)[0x55731b40f9b6] /opt/swift/bin/swift-frontend(+0xa410a7)[0x55731b40f0a7] /opt/swift/bin/swift-frontend(+0xa4341e)[0x55731b41141e] /opt/swift/bin/swift-frontend(+0xa4273d)[0x55731b41073d] /opt/swift/bin/swift-frontend(+0x914a39)[0x55731b2e2a39] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f138af1ad90] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f138af1ae40] /opt/swift/bin/swift-frontend(+0x914295)[0x55731b2e2295] Illegal instruction (core dumped)
But we got an error because the resultant value will exceed the maximum bound of Int8. So to overcome this problem we will use the addition overflow operator "&+". This operator will add them without giving an error.
import Foundation // Defining integer data type let num1 : Int8 = 110 let num2 : Int8 = 30 // Calculate the sum using the addition overflow operator var sum = num1 &+ num2 print("Sum of \(num1) and \(num2) = \(sum)")
Output
Sum of 110 and 30 = -116
Overflow Subtraction "&-" in Swift
The overflow Addition "&-" operator is used to subtract two integers with overflow checking. If any overflow occurs, then it will handle it properly without causing an error. It works with both signed and unsigned integers.
Syntax
Following is the syntax of the overflow subtraction operator −
var value = x &- b
Example
Swift program to subtract two unsigned integers.
import Foundation // Defining unsigned integer data type let num1 : UInt8 = 54 let num2 : UInt8 = 90 // Subtract integers using the normal subtraction operator var sum = num1 - num2 print("\(num1) - \(num2) = \(sum)")
Output
Stack dump: 0. Program arguments: /opt/swift/bin/swift-frontend -frontend -interpret main.swift -disable-objc-interop -color-diagnostics -new-driver-path /opt/swift/bin/swift-driver -empty-abi-descriptor -resource-dir /opt/swift/lib/swift -module-name main 1. Swift version 5.7.3 (swift-5.7.3-RELEASE) 2. Compiling with the current language version 3. While running user code "main.swift" Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it): /opt/swift/bin/swift-frontend(+0x551a103)[0x55736aebe103] /opt/swift/bin/swift-frontend(+0x551802e)[0x55736aebc02e] /opt/swift/bin/swift-frontend(+0x551a48a)[0x55736aebe48a] /lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f575ce12520] [0x7f575bac936c] /opt/swift/bin/swift-frontend(+0x1940b3d)[0x5573672e4b3d] /opt/swift/bin/swift-frontend(+0xc74db9)[0x557366618db9] /opt/swift/bin/swift-frontend(+0xa454c6)[0x5573663e94c6] /opt/swift/bin/swift-frontend(+0xa419b6)[0x5573663e59b6] /opt/swift/bin/swift-frontend(+0xa410a7)[0x5573663e50a7] /opt/swift/bin/swift-frontend(+0xa4341e)[0x5573663e741e] /opt/swift/bin/swift-frontend(+0xa4273d)[0x5573663e673d] /opt/swift/bin/swift-frontend(+0x914a39)[0x5573662b8a39] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f575cdf9d90] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f575cdf9e40] /opt/swift/bin/swift-frontend(+0x914295)[0x5573662b8295] Illegal instruction (core dumped)
Similarly, we got an error because it exceeded the bound. So to subtract two unsigned integers we will use the subtraction overflow "&-" operator which will subtract them even if their bound exceeds without giving any error.
import Foundation // Defining unsigned integer data type let num1 : UInt8 = 54 let num2 : UInt8 = 90 // Subtract integers using the subtraction overflow operator var sum = num1 &- num2 print("\(num1) - \(num2) = \(sum)")
Output
54 - 90 = 220
Overflow Multiplication(&*) in Swift
The overflow Multiplication "&*" operator is used to find the product of two integers with overflow checking. If any overflow occurs, then it will handle it properly without causing an error. It works with both signed and unsigned integers.
Syntax
Following is the syntax of the overflow multiplication operator −
var value = x &* b
Example
Swift program to calculate the product of two signed integers using multiplication overflow "&*" operator.
import Foundation // Defining integer data type let num1 : Int8 = 54 let num2 : Int8 = 90 // Calculate the product of two integers using the multiplication overflow operator var sum = num1 &* num2 print("Product of \(num1) and \(num2) = \(sum)")
Output
Product of 54 and 90 = -4