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>(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 sign-extending or truncating 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>(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(Float80)

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

init(NSNumber)Deprecated
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.

Performing Calculations

Integer Operators

Perform arithmetic and bitwise operations or compare values.

func negate()

Replaces this value with its additive inverse.

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)

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

func subtractingReportingOverflow(Int)

Returns the difference of this value and the given value along with a flag indicating whether overflow occurred in the operation.

func multipliedReportingOverflow(by: Int)

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

func dividedReportingOverflow(by: Int)

Returns the quotient of dividing this value by the given value along with a flag indicating whether overflow occurred in the operation.

func remainderReportingOverflow(dividingBy: Int)

Returns the remainder of dividing this value by the given value along with a flag indicating whether overflow occurred in the operation.

Performing Calculations Without Bounds Checks

These methods perform arithmetic operations without performing bounds checks. Use these methods only when you are sure the operation won't overflow.

func unsafeAdding(Int)

Returns the sum of this value and the given value without checking for arithmetic overflow.

func unsafeSubtracting(Int)

Returns the difference of this value and the given value without checking for arithmetic overflow.

func unsafeMultiplied(by: Int)

Returns the product of this value and the given value without checking for arithmetic overflow.

func unsafeDivided(by: Int)

Returns the quotient of dividing this value by the given value without checking for arithmetic overflow.

Finding the Sign and Magnitude

var magnitude: UInt

The magnitude of this value.

func abs<T>(T)

Returns the absolute value of the given number.

func signum()

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

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 words: Int.Words

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

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.

Working with Memory Addresses

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

init(bitPattern: ObjectIdentifier)

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

init<U>(bitPattern: UnsafePointer<U>?)

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

init<U>(bitPattern: UnsafeMutablePointer<U>?)

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

init(bitPattern: UnsafeRawPointer?)

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

init(bitPattern: UnsafeMutableRawPointer?)

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

init(bitPattern: OpaquePointer?)

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

Describing an Integer

var description: String

A textual representation of this value.

var hashValue: Int

The integer’s hash value.

var customMirror: Mirror

A mirror that reflects the Int instance.

See Also

Numeric Values

struct Double

A double-precision, floating-point value type.

struct Float

A single-precision, floating-point value type.