Understanding Swift Integer


Swift - Integers

In Swift, integers are used to store whole numbers. These numbers can be positive, negative, or zero. However, they do not store decimal or fractional values like 3.14 or 5.678. Swift categorizes integers into Signed Integers and Unsigned Integers to ensure flexibility and efficiency in programming.

Signed vs. Unsigned Integers in Swift

Signed Integers

Signed integers can hold both positive and negative values, including zero. Swift provides signed integers in 8-bit, 16-bit, 32-bit, and 64-bit sizes. The type representation is Int, such as Int8 for an 8-bit signed integer.

Unsigned Integers

Unsigned integers only store positive numbers and zero. They are available in 8-bit, 16-bit, 32-bit, and 64-bit formats. They are represented using UInt, like UInt32 for a 32-bit unsigned integer.

Int Data Type in Swift

Swift offers a special integer type called Int, which does not require an explicit size specification. The size of Int depends on the platform:

  • On a 32-bit system, Int is equivalent to Int32.
  • On a 64-bit system, Int is equivalent to Int64.

Syntax:

var number: Int

Example: Sum of Two Integers

import Foundation

let num1: Int = 232
let num2: Int = 31
var sum = num1 + num2

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

Output

Sum of 232 and 31 = 263

UInt Data Type in Swift

UInt is used to store only non-negative numbers without specifying their size. Like Int, the size of UInt depends on the platform:

  • On a 32-bit system, UInt equals UInt32.
  • On a 64-bit system, UInt equals UInt64.

Syntax:

var number: UInt

Example: Sum of Two Unsigned Integers

import Foundation

let num1: UInt = 32
let num2: UInt = 22
var sum = num1 + num2

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

Output

Sum of 32 and 22 = 54

Integer Size and Range in Swift

Type Size (Bytes) Range
Int8 1 -128 to 127
Int16 2 -32,768 to 32,767
Int32 4 -2,147,483,648 to 2,147,483,647
Int64 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
UInt8 1 0 to 255
UInt16 2 0 to 65,535
UInt32 4 0 to 4,294,967,295
UInt64 8 0 to 18,446,744,073,709,551,615

Finding Minimum and Maximum Integer Values in Swift

Swift provides built-in properties to determine the minimum and maximum values of different integer types.

Example: Get Minimum Values of Int8 and UInt16

import Foundation

let minInt8 = Int8. min
let minUInt16 = UInt16. min

print("Minimum Int8 value: \(minInt8) ")
print("Minimum UInt16 value: \(minUInt16) ")

Output

Minimum Int8 value: -128
Minimum UInt16 value: 0

Example: Get Maximum Values of Int16 and UInt64

import Foundation

let maxInt16 = Int16 .max
let maxUInt64 = UInt64 .max

print("Maximum Int16 value: \(maxInt16) ")
print("Maximum UInt64 value: \(maxUInt64) ")

Output

Maximum Int16 value: 32767
Maximum UInt64 value: 18446744073709551615

Why Use Integers in Swift?

  • Efficiency: Different integer sizes allow for memory-efficient coding.
  • Flexibility: Signed and unsigned integers provide a broad range of numerical values.
  • Performance: Swift optimizes integer calculations for high-speed execution.
  • Scalability: The platform-dependent Int and UInt make coding adaptable.