Mention what are the type of integers does Swift have?


In Swift, there are different types of integers −

  • Int − A signed integer with a size equal to the native word size of the computer (32 or 64 bits depending on the platform).

  • Signed integer types with a range of bit sizes include Int8, Int16, Int32, and Int64.

  • UInt − An unsigned integer type with the same native word size as the hardware (32 or 64 bits depending on the platform).

  • Unsigned integer types with designated bit sizes are UInt8, UInt16, UInt32, and UInt64.

Here are some examples of using each of the integer types in Swift −

Int

This is the most common number type used in the Swift language. When you declare an integer value in Swift, by default its type is Int. Here are some examples to declare int type −

Example

let a = 42 // int by default
let b: Int = -100
let c = Int.max
let d = Int.min
print("a: \(a), b: \(b), c: \(c), d: \(d)")

Output

a: 42, b: -100, c: 9223372036854775807, d: -9223372036854775808

Int8

You can use Int8 when you want to store a small range of numbers. e are some examples to declare Int8 type −

Example

let a: Int8 = 42
let b: Int8 = -100
let c = Int8.max
let d = Int8.min
print("a: \(a), b: \(b), c: \(c), d: \(d)")

Output

a: 42, b: -100, c: 127, d: -128

Int16

You can use Int16 when you want to store the integer numbers of 16-bits. These are some examples to declare Int16 type −

Example

let a: Int16 = 42
let b: Int16 = -100
let c = Int16.max
let d = Int16.min
print("a: \(a), b: \(b), c: \(c), d: \(d)")

Output

a: 42, b: -100, c: 32767, d: -32768

Int32

You can use Int32 when you want to store the integer numbers of 32-bits. These are some examples to declare Int32 type −

Example

let a: Int32 = 42
let b: Int32 = -100
let c = Int32.max
let d = Int32.min
print("a: \(a), b: \(b), c: \(c), d: \(d)")

Output

a: 42, b: -100, c: 2147483647, d: -2147483648

Int64

You can use Int64 when you want to store the integer numbers of 64-bits. These are some examples to declare Int64 type −

Example

let a: Int64 = 42
let b: Int64 = -100
let c = Int64.max
let d = Int64.min
print("a: \(a), b: \(b), c: \(c), d: \(d)")

Output

a: 42, b: -100, c: 9223372036854775807, d: -9223372036854775808

UInt

You can use UInt when you want to store unsigned integer numbers. These are some examples to declare UInt type −

Example

let a = 42 // UInt by default
let b: UInt = 100
let c = UInt.max
print("a: \(a), b: \(b), c: \(c)")

Output

a: 42, b: 100, c: 18446744073709551615

There are several types of integer data types. You can choose integers according to your requirements to perform mathematical operations, comparisons, and other operations as well. For unsigned values, you can use the UInt integer type. You can convert the integer number from one to another type.

In addition to the types of integers we mentioned earlier, Swift also has several other types and features related to integers −

  • Int and UInt − These are the most typical integer types used in Swift. They can represent a wide range of positive and negative values and are the same size as the machine's native word size (32 or 64 bits depending on the platform).

  • Int8, Int16, Int32, Int64 and UInt8, UInt16, UInt32, UInt64 − These are different bit sizes for signed and unsigned integer types. They are helpful when interacting with external systems that have certain constraints for the size of integers and can be used to represent numbers within a specific range.

  • Integer literals − Decimal, binary, octal, and hexadecimal integer literals are all supported in Swift. For instance, you may write 42 for the integer in decimal form, 0b101010 for the integer in binary form, 052 for the integer in octal form, and 0x2A for the number in hexadecimal form.

  • Integer overflow and underflow − The terms "overflowed" and "underflowed" are used to describe when an operation on an integer produces a number that is outside the range of the integer type. Options are available in Swift to deal with these circumstances, including the checked and wrapping overflow operators.

  • Bitwise operations − To perform operations on the individual bits of an integer, Swift offers a collection of bitwise operators, including &, |, ^, ~, etc.

  • Integer conversion − The as operator, the & (and-assignment) operator, and the init(_:) initializer are just a few of the ways that Swift makes it possible to switch between integer types.

Overall, Swift provides a wide range of options for working with integers. This includes various types with specific ranges and capabilities, as well as a set of operators and features to make working with integers more convenient.

Conclusion

Swift has a variety of integer types, including Int, Int8, Int16, Int32, Int64, and UInt, UInt8, UInt16, Int32, and UInt64, for encoding whole numbers. These kinds are helpful in many contexts and each has a defined set of values that it can represent.

For instance, types having defined widths like Int8, and UInt16 are useful when interacting with external systems that have specific requirements for the size of integers. Int and UInt are suitable for general-purpose integer operations. To make working with integers more convenient, Swift also includes tools like integer literals, bitwise operations, and integer conversion.

Updated on: 28-Feb-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements