Swift - Integers


Integers data type is used to store whole numbers such as 23, -543, 0, 332, etc. It can store positive, negative and zero numbers. It does not store fractional numbers like 3.43, 4.423, etc. Swift divides integers into two categories −

  • Signed Integers − Signed integers are used to store zero and positive integers. They are available in 8, 16, 23, and 64-bit forms. They are represented by Int like an 8-bit signed integer of type Int8.

  • Unsigned Integers − Unsigned integers are used to store negative integers. They are also available in 8, 16, 23, and 64-bit forms. It is represented by Uint like a 32-bit unsigned integer of type UInt32.

Int

Swift provides a special integer type named Int. Using Int type we do not need to explicitly specify the size of the integer. The size of Int is the same as the size of the platform such as if we have a 32-bit platform then the size of Int is Int32 and if we have a 64-bit platform then the size of the Int is Int64. It can store values between -2, 147, 483, 648 and 2, 147, 483, 647.

Syntax

Following is the syntax of the Int −

var value : Int

Example

Swift program to calculate the sum of two integers.

import Foundation

// Defining integer data type
let num1 : Int = 232
let num2 : Int = 31

// Store the sum
var sum = 0

// Calculate the sum 
sum = num1 + num2

print("Sum of \(num1) and \(num2) = \(sum)")

Output

Sum of 232 and 31 = 263

UInt

Using UInt we can also store unsigned integer data types without explicitly specifying their size. The size of Uint is also the same as the size of the platform such as if we have a 32-bit platform, then the size is UInt32 whereas if we have a 64-bit platform, then the size is UInt64.

Syntax

Following is the syntax of UInt −

var num : Uint = 32

Example

Swift program to add two unsigned integers.

import Foundation

// Defining Unsigned integer data type
let num1 : UInt = 32
let num2 : UInt = 22

// Store the sum
var sum : UInt

// Calculate the sum 
sum = num1 + num2
print("Sum of \(num1) and \(num2) = \(sum)")

Output

Sum of 32 and 22 = 54

Integer Bounds

The minimum and maximum size of the integer data type are as follows −

Type Size Range
Int8 1 bytes -128 to 127
Int16 2 bytes -32768 to 32767
Int32 4 bytes -2147483648 to 2147483647
Int64 8 bytes -9223372036854775808 to 9223372036854775807
UInt8 1 bytes 0 to 255
UInt16 2 bytes 0 to 65535
UInt32 4 bytes 0 to 4294967295
UInt64 8 bytes 0 to 18446744073709551615

Minimum and Maximum size of Integer

We can explicitly calculate the size of the integer with the help of the pre-defined properties of Swift min and max.

The min property is used to calculate the minimum size of the integer and the max property is used to calculate the maximum size of the integer.

Example

Swift program to calculate the minimum size of the Int8 and UInt16.

import Foundation

// Minimum size of Int8 and UInt16
let result1 = Int8.min
let result2 = UInt16.min

print("Minimum Size of Int8 is \(result1)")
print("Minimum Size of UInt16 is \(result2)")

Output

Minimum Size of Int8 is -128
Minimum Size of UInt16 is 0

Example

Swift program to calculate the maximum size of the Int16 and UInt64.

import Foundation

// Maximum size of Int16 and UInt64
let result1 = Int16.max
let result2 = UInt64.max

print("Maximum Size of Int16 is \(result1)")
print("Maximum Size of UInt64 is \(result2)")

Output

Maximum Size of Int16 is 32767
Maximum Size of UInt64 is 18446744073709551615

Integer Type Conversion

Generally, when we are converting a smaller data type to a larger one (of the same kind), many languages perform implicit conversion. This ensures type safety and prevents data loss.

Swift doesn't support (this) implicit type conversion therefore, we need to convert the values between different integer types explicitly.

Syntax

Following is the syntax for converting one integer type to another −

let smallNumber: UInt8 = 100
let largerNumber: UInt16 = UInt16(smallNumber)

Example

Swift program to demonstrate integer type conversion −

import Foundation

let a: Int8 = 42
let b: Int16 = Int16(a)  // Convert Int8 to Int16

print("Converted value: \(b)")

The output of the above example is:

Converted value: 42

Integer Arithmetic Operations and Overflow Handling

Swift supports common arithmetic operations with overflow handling using the arithmetic overflow operators.

Syntax

Following is the syntax for overflow-safe operations in Swift −

let result = a &+ b // Addition with overflow
let result = a &- b // Subtraction with overflow
let result = a &* b // Multiplication with overflow

Example

Swift program demonstrating overflow handling using overflow operators −

import Foundation

let maxVal: UInt8 = 255
let overflowResult = maxVal &+ 1

print("Overflow Result: \(overflowResult)")

The output of the above example is:

Overflow Result: 0

Integer Literals in Different Bases

We can also represent integer values using different literals in binary, octal, decimal, and hexadecimal formats by using specific prefixes (0b for Binary, 0o for Octal, and 0x for Hexadecimal).

Syntax

Following is the syntax to define literals in different bases −

// Binary (prefix 0b)
let binaryNum = 0b1010

// Octal (prefix 0o)
let octalNum = 0o17

// Decimal (default)
let decimalNum = 42

// Hexadecimal (prefix 0x)
let hexNum = 0x2A

Example

Swift program to demonstrate integer literals in various number systems −

import Foundation

let binary = 0b1101
let octal = 0o32
let decimal = 26
let hex = 0x1A

print("Binary: \(binary)")
print("Octal: \(octal)")
print("Decimal: \(decimal)")
print("Hexadecimal: \(hex)")

The output of the above example is:

Binary: 13
Octal: 26
Decimal: 26
Hexadecimal: 26