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.

Int32 is a value type in Swift representing a fixed-width, 32-bit signed integer. It stores whole numbers using two’s complement binary representation, allocating exactly 4 bytes of memory regardless of the underlying hardware architecture’s word size.

Value Range and Bounds

Because Int32 is a signed 32-bit integer, it utilizes 1 bit for the sign and 31 bits for the magnitude. Its bounds are strictly defined and can be accessed via static properties:
let minValue: Int32 = Int32.min // -2,147,483,648 (-2^31)
let maxValue: Int32 = Int32.max //  2,147,483,647 (2^31 - 1)

Initialization and Type Conversion

Swift is a strongly typed language and does not perform implicit type conversions. Int32 cannot be implicitly cast to or from other integer types (like Int, Int8, or Int64), even if the value safely fits within the 32-bit range.
// Literal initialization
let explicitInt32: Int32 = 42

// Explicit conversion from standard Int
let standardInt: Int = 100
let convertedInt32 = Int32(standardInt)

// Explicit conversion from Int32 to Int64
let widenedInt = Int64(explicitInt32)
If a standard initialization attempt exceeds the 32-bit bounds, Swift triggers a runtime error. To handle potential out-of-bounds conversions safely, Int32 provides an exact failable initializer:
let largeValue: Int64 = 5_000_000_000
let safeInt32 = Int32(exactly: largeValue) // Returns nil

Memory and Bitwise Characteristics

As a type conforming to FixedWidthInteger, Int32 exposes its binary characteristics directly. You can inspect its bit width and manipulate its memory representation using bitwise operators.
let bitCount = Int32.bitWidth // 32
    
let value: Int32 = 8
let leadingZeros = value.leadingZeroBitCount // 28
let trailingZeros = value.trailingZeroBitCount // 3
    
// Accessing the underlying two's complement bit pattern
let bitPattern: UInt32 = UInt32(bitPattern: -1) // 4294967295 (0xFFFFFFFF)

Overflow Behavior

By default, arithmetic operations on Int32 trap (crash) if the result exceeds the 32-bit bounds. To perform modular arithmetic that wraps around the 32-bit boundary without trapping, Swift provides specific masking operators (&+, &-, &*).
var max = Int32.max

// Standard addition: traps on overflow
// max += 1 // Fatal error: arithmetic overflow

// Masking addition: wraps to Int32.min
max = max &+ 1 // -2,147,483,648

Protocol Conformances

Int32 is implemented as a struct in the Swift Standard Library and conforms to a robust hierarchy of numeric and utility protocols, including:
  • SignedInteger & FixedWidthInteger: Enabling bitwise operations, bit shifts, and sign-aware arithmetic.
  • Hashable & Equatable: Allowing instances to be hashed for collection storage (like Set or Dictionary keys) and evaluated for equality.
  • Comparable: Enabling sorting and relational operators (<, >, <=, >=).
  • Codable: Supporting native serialization and deserialization to formats like JSON or Property Lists.
  • Strideable: Allowing Int32 to be used in ranges and loops with specific step increments.
Master Swift with Deep Grasping Methodology!Learn More