Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Int64 is a value type in the Swift Standard Library representing a fixed-width, 64-bit signed integer. It allocates exactly 8 bytes of memory to store numerical values using two’s complement binary representation, allowing it to represent both positive and negative whole numbers within a strictly defined, immutable range.

Memory and Bounds

As a 64-bit signed integer utilizing two’s complement representation, the most significant bit (MSB) functions as the sign bit (0 for positive, 1 for negative). For positive values, the lower 63 bits represent the binary magnitude. For negative values, the bit pattern represents the bitwise NOT of the magnitude plus one. This encoding ensures a single representation of zero and allows the arithmetic logic unit (ALU) to perform addition and subtraction without distinguishing between positive and negative operands.
  • Minimum Value: 263-2^{63} (-9,223,372,036,854,775,808)
  • Maximum Value: 26312^{63} - 1 (9,223,372,036,854,775,807)
You can access these boundaries programmatically using static properties:
let lowestValue: Int64 = Int64.min
let highestValue: Int64 = Int64.max

Type Safety and Conversion

Swift enforces strict type safety. Int64 is a distinct type from the platform-dependent Int. Even on 64-bit architectures (where Int is also 64 bits wide), the compiler treats Int and Int64 as entirely separate types. Implicit casting is not permitted; you must explicitly initialize an Int64 when converting from other numeric types.
let platformInt: Int = 42
let explicitInt64: Int64 = 42

// let typeMismatch: Int64 = platformInt // Compiler Error: Cannot convert value of type 'Int' to specified type 'Int64'

let convertedInt64 = Int64(platformInt)  // Valid explicit conversion

Protocol Conformances

Int64 is implemented as a struct and conforms to a robust hierarchy of numeric protocols, which dictate its available operations:
  • FixedWidthInteger: Guarantees a specific bit width and provides endianness conversions (bigEndian, littleEndian) and bitwise operations.
  • SignedInteger: Requires the type to represent both positive and negative values.
  • BinaryInteger: Provides the foundation for all integer types, enabling bit-shift operations (<<, >>) and quotient/remainder division.
  • Hashable, Equatable, Comparable: Allows Int64 to be used as dictionary keys, compared using standard relational operators (==, <, >), and sorted.

Overflow and Underflow Behavior

By default, Swift arithmetic operators (+, -, *) trap (trigger a runtime crash) if an operation exceeds the bounds of Int64. To perform modulo arithmetic that wraps around the bounds instead of crashing, Int64 supports Swift’s overflow operators (&+, &-, &*). Note that the compiler performs constant folding on literal arithmetic. To observe a runtime trap rather than a compile-time error, the overflow must occur during runtime evaluation:
var currentVal: Int64 = Int64.max

// currentVal += 1 // Runtime execution trap (Overflow)

let wrappedVal: Int64 = currentVal &+ 1 // Wraps to Int64.min (-9,223,372,036,854,775,808)

Bitwise Operations

As a FixedWidthInteger, Int64 supports direct bit manipulation. Operations are applied across all 64 bits.
let a: Int64 = 0b1010_1010
let b: Int64 = 0b0101_0101

let bitwiseAnd = a & b  // 0b0000_0000
let bitwiseOr  = a | b  // 0b1111_1111
let bitwiseXor = a ^ b  // 0b1111_1111
let bitShift   = a << 2 // 0b10_1010_1000
Master Swift with Deep Grasping Methodology!Learn More