Selected Reading

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 both positive and negative whole numbers, including zero. They use one bit to represent the sign (positive or negative), typically using two’s complement representation.

    They are available in standard sizes such as 8-bit, 16-bit, 32-bit, and 64-bit. These are represented as Int8, Int16, Int32, and Int64. For example, an 8-bit signed integer (Int8) can store values from −128 to 127.

  • Unsigned Integers − Unsigned integers are used to store non-negative whole numbers (0 and positive values only). They do not support negative values because no bit is reserved for the sign.

    They are available in standard sizes such as 8-bit, 16-bit, 32-bit, and 64-bit. These are represented as UInt8, UInt16, UInt32, and UInt64. For example, a 32-bit unsigned integer (UInt32) can store values from 0 to 4,294,967,295.

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. For example, on a 32-bit platform, Int is equivalent to Int32, and on a 64-bit platform, Int is equivalent to Int64.

Syntax

var value : Int

Example

Swift program to calculate the sum of two integers.

import Foundation

let num1 : Int = 232
let num2 : Int = 31

var sum = 0

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 values without explicitly specifying their size. The size of UInt is also the same as the size of the platform. For example, on a 32-bit platform, UInt is equivalent to UInt32, whereas on a 64-bit platform, UInt is equivalent to UInt64.

Syntax

var num : UInt = 32

Example

Swift program to add two unsigned integers.

import Foundation

let num1 : UInt = 32
let num2 : UInt = 22

var sum : UInt

sum = num1 + num2

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

Output

Sum of 32 and 22 = 54

Integer Bounds

The minimum and maximum values of the integer data types are as follows −

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

Minimum and Maximum Values of Integers

We can explicitly calculate the minimum and maximum values of integers with the help of the pre-defined properties of Swift min and max.

Example

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

import Foundation

let result1 = Int8.min
let result2 = UInt16.min

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

Output

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

Example

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

import Foundation

let result1 = Int16.max
let result2 = UInt64.max

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

Output

Maximum value of Int16 is 32767
Maximum value of UInt64 is 18446744073709551615

Integer Type Conversion

Many languages perform implicit conversion, but Swift does not support implicit type conversion. Therefore, we must convert values between different integer types explicitly to ensure type safety and prevent data loss.

Syntax

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

Example

import Foundation

let a: Int8 = 42
let b: Int16 = Int16(a)

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

Integer Arithmetic Operations and Overflow Handling

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

Syntax

let result = a &+ b
let result = a &- b
let result = a &* b

Example

import Foundation

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

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

Integer Literals in Different Bases

We can represent integer values using binary, octal, decimal, and hexadecimal formats using prefixes (0b for binary, 0o for octal, and 0x for hexadecimal).

Example

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)")
Binary: 13
Octal: 26
Decimal: 26
Hexadecimal: 26
Advertisements