”;
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
”;