Chapter 7 : Data-types Integers

Topics Covered :

1. What are Integers
2. Examples

# Integers

Integers are whole numbers which can be signed, unsigned but not fractions.

What is a Integers

Examples : -10, 30, 0

Swift supports both signed and unsigned integers.The forms supported are 8-bits,16-bits,32-bits and 64-bits.

Example :
Syntax : let constantName = value

``````
//
//  main.swift
//  Swift Tutorials 2014-15
//  Integers in Swift
//
//  Created by Code2care on 15/09/14.
//

var a = 10
var b = -20

var val1 = UInt8 (10)
var val2 = UInt16 (4006)
var val3 = UInt32 (32523)
var val4 = UInt64 (58455746)

println("\(a)")
println("\(b)")

println("\(val1)")
println("\(val2)")
println("\(val3)")
println("\(val4)")

```
```
Output :

10
-20
10
4006
32523
58455746
Program ended with exit code: 0

### Signed Integers

Lets find the range for Signed Integer types,

``````
//
//  main.swift
//  Swift Tutorials 2014-15
//  Signed Integers in Swift
//
//  Created by Code2care on 15/09/14.
//

println(" Int8 => min : \(Int8.min) max :  \(Int8.max)")
println(" Int16 => min : \(Int16.min) max :  \(Int16.max)")
println(" Int32 => min : \(Int32.min) max :  \(Int32.max)")
println(" Int64 => min : \(Int64.min) max :  \(Int64.max)")

```
```
Output :

Int8 => min : -128 max : 127
UInt16 => min : -32768 max : 32767
UInt32 => min : -2147483648 max : 2147483647
UInt64 => min : -9223372036854775808 max : 9223372036854775807
Program ended with exit code: 0

Integer type Int which on 32-bit platforms has a size that of Int32 and Int64 on a 64-bit platform. It is recommended to use Int in your program.

### UnSigned Integers (UInt)

If you are sure that the Integer cannot have a negative value you may use a Unsigned Integer,

``````
//
//  main.swift
//  Swift Tutorials 2014-15
//  Unsigned Integers in Swift
//
//  Created by Code2care on 15/09/14.
//

println(" UInt8 => min : \(UInt8.min) max :  \(UInt8.max)")
println(" UInt16 => min : \(UInt16.min) max :  \(UInt16.max)")
println(" UInt32 => min : \(UInt32.min) max :  \(UInt32.max)")
println(" UInt64 => min : \(UInt64.min) max :  \(UInt64.max)")

```
```
Output :

UInt8 => min : 0 max : 255
UInt16 => min : 0 max : 65535
UInt32 => min : 0 max : 4294967295
UInt64 => min : 0 max : 18446744073709551615
Program ended with exit code: 0