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.

UInt is a platform-dependent, unsigned integer value type in Swift. It represents non-negative whole numbers (zero and positive integers) and automatically sizes itself to the native word size of the execution environment: 32 bits on a 32-bit platform and 64 bits on a 64-bit platform.

Memory and Bounds

Because UInt is unsigned, its memory allocation is entirely dedicated to representing positive magnitude, omitting the sign bit used in signed integers (Int). You can query the absolute limits of the type on the current platform using the min and max static properties:
let lowestValue = UInt.min  // Always 0
let highestValue = UInt.max // 2^32 - 1 on 32-bit, 2^64 - 1 on 64-bit

Type Safety and Conversion

Swift’s type system is strictly enforced. UInt is a distinct type from Int (the default integer type in Swift) and other explicitly sized integer types. Swift does not perform implicit type coercion between signed and unsigned integers, even if the underlying value is positive and safely within bounds. To assign an Int to a UInt, or vice versa, you must perform an explicit type conversion:
let signedInteger: Int = 256

// let unsignedInteger: UInt = signedInteger 
// ^ Compiler Error: Cannot convert value of type 'Int' to specified type 'UInt'

let unsignedInteger: UInt = UInt(signedInteger) // Explicit initialization
If an explicit conversion is attempted with a negative Int value using the standard initializer, it will result in a runtime crash, as UInt cannot represent values below zero. To safely reinterpret the exact memory representation of a signed integer as an unsigned integer without trapping, use the init(bitPattern:) initializer. This mechanism bypasses bounds checking and directly maps the underlying binary sequence:
let negativeInt: Int = -1

// let crashedUInt = UInt(negativeInt) 
// ^ Runtime crash: integer out of range

let bitPatternUInt = UInt(bitPattern: negativeInt) 
// Safely reinterprets the bits; results in UInt.max

Overflow and Underflow Behavior

By default, Swift arithmetic operators (+, -, *) are overflow-safe. If an operation results in a value outside the bounds of UInt (e.g., dropping below 0 or exceeding UInt.max), the program will trap and crash at runtime to prevent undefined behavior. To intentionally permit overflow or underflow, Swift provides overflow operators (also known as wrapping operators) (&+, &-, &*):
var unsignedZero: UInt = 0

// unsignedZero -= 1 
// ^ Runtime crash: arithmetic operation results in an underflow

let wrappedUnderflow = unsignedZero &- 1 
// Wraps around to UInt.max

var unsignedMax: UInt = UInt.max
let wrappedOverflow = unsignedMax &+ 1 
// Wraps around to 0

Explicitly Sized Variants

While UInt dynamically adapts to the platform’s architecture, Swift also provides explicitly sized unsigned integer structs for scenarios requiring strict memory footprints or binary data manipulation. These types are independent of the platform’s word size:
  • UInt8: 8-bit unsigned integer (0 to 255)
  • UInt16: 16-bit unsigned integer (0 to 65,535)
  • UInt32: 32-bit unsigned integer (0 to 4,294,967,295)
  • UInt64: 64-bit unsigned integer (0 to 18,446,744,073,709,551,615)
Like Int, UInt requires explicit conversion when interacting with any of these explicitly sized variants.
Master Swift with Deep Grasping Methodology!Learn More