Structure

# Int

A signed integer value type.

## Overview

On 32-bit platforms, `Int` is the same size as `Int32`, and on 64-bit platforms, `Int` is the same size as `Int64`.

## Topics

### Converting Integers

`init<T>(T)`

Creates a new instance from the given integer.

`init?<T>(exactly: T)`

Creates a new instance from the given integer, if it can be represented exactly.

`init<Other>(clamping: Other)`

Creates a new instance with the representable value that’s closest to the given integer.

`init<T>(truncatingIfNeeded: T)`

Creates a new instance from the bit pattern of the given instance by truncating or sign-extending if needed to fit this type.

`init(bitPattern: UInt)`

Creates a new instance with the same memory representation as the given value.

### Converting Floating-Point Values

`init<T>(T)`

Creates an integer from the given floating-point value, rounding toward zero. Any fractional part of the value passed as `source` is removed.

`init?<T>(exactly: T)`

Creates an integer from the given floating-point value, if it can be represented exactly.

`init(Double)`

Creates an integer from the given floating-point value, rounding toward zero.

`init(Float)`

Creates an integer from the given floating-point value, rounding toward zero.

`init(CGFloat)`

Creates an integer from the given floating-point value, rounding toward zero.

`init(Float80)`

Creates an integer from the given floating-point value, rounding toward zero.

`init?(exactly: Double)`

Creates an integer from the given floating-point value, if it can be represented exactly.

`init?(exactly: Float)`

Creates an integer from the given floating-point value, if it can be represented exactly.

`init?(exactly: Float80)`

Creates an integer from the given floating-point value, if it can be represented exactly.

### Converting Strings

`init?(String)`

Creates a new integer value from the given string.

`init?<S>(S, radix: Int)`

Creates a new integer value from the given string and radix.

### Creating a Random Integer

`static func random(in: Range<Int>) -> Int`

Returns a random value within the specified range.

`static func random<T>(in: Range<Int>, using: inout T) -> Int`

Returns a random value within the specified range, using the given generator as a source for randomness.

`static func random(in: ClosedRange<Int>) -> Int`

Returns a random value within the specified range.

`static func random<T>(in: ClosedRange<Int>, using: inout T) -> Int`

Returns a random value within the specified range, using the given generator as a source for randomness.

### Performing Calculations

Integer Operators

Perform arithmetic and bitwise operations or compare values.

`func negate()`

Replaces this value with its additive inverse.

`func quotientAndRemainder(dividingBy: Int) -> (quotient: Int, remainder: Int)`

Returns the quotient and remainder of this value divided by the given value.

`func isMultiple(of: Int) -> Bool`

Returns `true` if this value is a multiple of the given value, and `false` otherwise.

### Performing Calculations with Overflow

These methods return the result of an operation, and a flag indicating whether the operation overflowed the bounds of the type.

`func addingReportingOverflow(Int) -> (partialValue: Int, overflow: Bool)`

Returns the sum of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.

`func subtractingReportingOverflow(Int) -> (partialValue: Int, overflow: Bool)`

Returns the difference obtained by subtracting the given value from this value, along with a Boolean value indicating whether overflow occurred in the operation.

`func multipliedReportingOverflow(by: Int) -> (partialValue: Int, overflow: Bool)`

Returns the product of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.

`func dividedReportingOverflow(by: Int) -> (partialValue: Int, overflow: Bool)`

Returns the quotient obtained by dividing this value by the given value, along with a Boolean value indicating whether overflow occurred in the operation.

`func remainderReportingOverflow(dividingBy: Int) -> (partialValue: Int, overflow: Bool)`

Returns the remainder after dividing this value by the given value, along with a Boolean value indicating whether overflow occurred during division.

### Performing Double-Width Calculations

`func multipliedFullWidth(by: Int) -> (high: Int, low: Int.Magnitude)`

Returns a tuple containing the high and low parts of the result of multiplying this value by the given value.

`func dividingFullWidth((high: Int, low: Int.Magnitude)) -> (quotient: Int, remainder: Int)`

Returns a tuple containing the quotient and remainder of dividing the given value by this value.

### Finding the Sign and Magnitude

`var magnitude: UInt`

The magnitude of this value.

`typealias Int.Magnitude`

A type that can represent the absolute value of any possible value of this type.

`func abs<T>(T) -> T`

Returns the absolute value of the given number.

`func signum() -> Int`

Returns `-1` if this value is negative and `1` if it’s positive; otherwise, `0`.

### Accessing Numeric Constants

`static var min: Int`

The minimum representable integer in this type.

`static var max: Int`

The maximum representable integer in this type.

`static var isSigned: Bool`

A Boolean value indicating whether this type is a signed integer type.

### Working with Byte Order

`var byteSwapped: Int`

A representation of this integer with the byte order swapped.

`var littleEndian: Int`

The little-endian representation of this integer.

`var bigEndian: Int`

The big-endian representation of this integer.

`init(littleEndian: Int)`

Creates an integer from its little-endian representation, changing the byte order if necessary.

`init(bigEndian: Int)`

Creates an integer from its big-endian representation, changing the byte order if necessary.

### Working with Binary Representation

`static var bitWidth: Int`

The number of bits used for the underlying binary representation of values of this type.

`var bitWidth: Int`

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

`var nonzeroBitCount: Int`

The number of bits equal to 1 in this value’s binary representation.

`var leadingZeroBitCount: Int`

The number of leading zeros in this value’s binary representation.

`var trailingZeroBitCount: Int`

The number of trailing zeros in this value’s binary representation.

`var words: Int.Words`

A collection containing the words of this value’s binary representation, in order from the least significant to most significant.

`struct Int.Words`

A type that represents the words of this integer.

These initializers create an integer with the bit pattern of the memory address of a pointer or class instance.

`init<P>(bitPattern: P?)`

Creates a new value with the bit pattern of the given pointer.

`init(bitPattern: ObjectIdentifier)`

Creates an integer that captures the full value of the given object identifier.

`init(bitPattern: OpaquePointer?)`

Creates a new value with the bit pattern of the given pointer.

### Encoding and Decoding Values

`func encode(to: Encoder)`

Encodes this value into the given encoder.

`init(from: Decoder)`

Creates a new instance by decoding from the given decoder.

### Describing an Integer

`var description: String`

A textual representation of this value.

`func hash(into: inout Hasher)`

Hashes the essential components of this value by feeding them into the given hasher.

`var customMirror: Mirror`

A mirror that reflects the `Int` instance.

### Using an Integer as a Data Value

`init?(from: MLDataValue)`

Creates an integer from another integer wrapped in a data value.

`var dataValue: MLDataValue`

The integer wrapped in a data value.

`static var dataValueType: MLDataValue.ValueType`

The underlying type an integer uses when it wraps itself in a data value.

### Infrequently Used Functionality

`init()`

Creates a new value equal to zero.

`init(integerLiteral: Int)`

Creates an instance initialized to the specified integer value.

`typealias Int.IntegerLiteralType`

A type that represents an integer literal.

`func distance(to: Int) -> Int`

Returns the distance from this value to the given value, expressed as a stride.

`func advanced(by: Int) -> Int`

Returns a value that is offset the specified distance from this value.

### Deprecated

`var customPlaygroundQuickLook: _PlaygroundQuickLook`

A custom playground Quick Look for the `Int` instance.

Deprecated

### SIMD-Supporting Types

`struct Int.SIMD2Storage`

Storage for a vector of two integers.

`struct Int.SIMD4Storage`

Storage for a vector of four integers.

`struct Int.SIMD8Storage`

Storage for a vector of eight integers.

`struct Int.SIMD16Storage`

Storage for a vector of 16 integers.

`struct Int.SIMD32Storage`

Storage for a vector of 32 integers.

`struct Int.SIMD64Storage`

Storage for a vector of 64 integers.

### Type Aliases

`typealias Int.Stride`

A type that represents the distance between two values.

### Instance Properties

`var identifierValue: MLDataValue`

The integer value of the unique identifier wrapped in a data value.

### Standard Library

`struct Double`

A double-precision, floating-point value type.

`struct String`

A Unicode string value that is a collection of characters.

`struct Array`

An ordered, random-access collection.

`struct Dictionary`

A collection whose elements are key-value pairs.

Swift Standard Library

Solve complex problems and write high-performance, readable code.