Int by Example - Swift Programming Language

Overview

A signed integer value type. On 32-bit platforms, Int is the same size as Int32, and on 64-bit platforms, Int is the same size as Int64.

1. Declaring Constants and Variables

  • Constants and variables must be declared before they’re used. You declare constants with the let keyword and variables with the var keyword.
let constantNum = 10
var variableNum = 10
print("constant data type value: \(constantNum)")
print("variable data type value before change :\(variableNum)")
variableNum = 40
print("variable data type value after change :\(variableNum)")
//prints constant data type value: 10
//variable data type value before change :10
//variable data type value after change :40

2. Type Annotations

  • You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store.
  • Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.
let number1 : Int = 100
print(number1)

3. Naming Constants and Variables

  • Constant and variable names can contain almost any character, including Unicode characters.
let n = 10
print(n)
let 你好 = 145
print(你好)
let 🐶🐮 = 20000
print(🐶🐮)
//prints 10
// 145
// 20000

4. Converting Floating-Point Values

  • Creates an integer from the given floating-point value, rounding toward zero.
let x = Int(21.5)
print(x)
let y = Int(-21.5)
print(y)
//prints 21
//prints -21

5. Performing Calculations

5.1 Integer Operators

  • Perform arithmetic and bitwise operations or compare values.
let x = Int(21.5)
let y = Int(-21.5)
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x >> y)
print(x ^ y)
//prints 0
// 42
// -441
// -1
// 44040192
// -2

5.2 negate()

  • Replaces this value with its additive inverse.
var z = 30
print(z)
z.negate()
print(z)
//prints 30
//prints -30

6. Finding the Sign and Magnitude

6.1 magnitude

  • The magnitude of this value.
let y = -200
print(y.magnitude)
//prints 200

6.2 abs(_:)

  • Returns the absolute value of the given number.
let y = -200
print(abs(y))
//prints 200

6.3 signum()

  • Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
let y = -200
print(y.signum())
//prints -1

7. Describing an Integer

7.1 description

  • A textual representation of this value.
let y = -200
print(y.description)
//prints -200

7.2 hashValue

  • The integer’s hash value.
let y = -200
print(y.hashValue)

7.3 customMirror

  • A mirror that reflects the Int instance.
let y = -200
print(y.customMirror)
// prints Mirror for Int

7.4 customPlaygroundQuickLook

let y = -200
print(y.customPlaygroundQuickLook)
//prints int(-200)

8. Instance Properties - words

  • A collection containing the words of this value’s binary representation, in order from the least significant to most significant.
let y = -200
print(y.words)
//prints Words(_value: -21)

9. Infrequently Used Functionality

9.1 distance(to:)

let y = -200
print(y.distance(to: 2))
//prints 202

9.2 advanced(by:)

let y = -200
print(y.advanced(by: 3))
//prints -197

10. Working with Binary Representation

10.1 bitwidth

The number of bits in the binary representation of this value.

let y = 200
print(y.bitWidth)
//prints 64

10.2 nonzeroBitCount

  • The number of bits equal to 1 in this value’s binary representation.
let l: Int8 = 0b0001_1111
print(l.nonzeroBitCount)
//prints 5

10.3 leadingZeroBitCount

  • The number of leading zeros in this value’s binary representation.
let l: Int8 = 0b0001_1111
print(l.leadingZeroBitCount)
//prints 3

10.4 trailingZeroBitCount

  • The number of trailing zeros in this value’s binary representation.
let l: Int8 = 0b0001_1111
print(l.trailingZeroBitCount) 
//prints 0

11. Working with Byte Order

11.1 byteSwapped

  • A representation of this integer with the byte order swapped.
let l: Int8 = 0b0001_1111
print(l.byteSwapped)
//prints 31

11.2 littleEndian

let l: Int8 = 0b0001_1111
print(l.littleEndian)
//prints 31

11.3 bigEndian

let l: Int8 = 0b0001_1111
print(l.bigEndian)
//prints 31

You can download the swift playground of all above examples from Here




Related Article