Skip to main content
UInt64 is a value type in Swift representing a 64-bit unsigned integer. It stores non-negative whole numbers using exactly 8 bytes of memory, providing a fixed-width numeric representation that remains strictly 64 bits wide regardless of the underlying platform’s architecture (unlike UInt, which scales to the platform’s word size). Because it is an unsigned type, it allocates all 64 bits to the magnitude of the number, omitting the sign bit. This yields the following mathematical bounds:
  • Minimum Value: 0
  • Maximum Value: 18,446,744,073,709,551,615 (26412^{64} - 1)

Syntax and Initialization

You can instantiate a UInt64 using integer literals, explicit type annotation, or by converting from other numeric types. Swift does not implicitly convert between integer types, so typecasting is strictly required when mixing UInt64 with types like Int or UInt32.

Overflow and Underflow Behavior

By default, Swift prevents integer overflow and underflow. If an arithmetic operation on a UInt64 results in a value outside its 64-bit bounds, the program will trigger a runtime trap. To intentionally allow wrapping behavior at the bit level, Swift provides masking arithmetic operators (&+, &-, &*).

Protocol Conformances

Under the hood, UInt64 is implemented as a struct. It conforms to a robust hierarchy of Swift standard library protocols, dictating its capabilities:
  • FixedWidthInteger & BinaryInteger: Guarantees a specific bit width, enabling bitwise operations (~, &, |, ^, <<, >>) and providing access to properties like leadingZeroBitCount and nonzeroBitCount.
  • UnsignedInteger: Semantically enforces the lack of a sign bit.
  • Hashable & Equatable: Allows UInt64 to be compared for equality (==, !=) and used as keys in Dictionary or elements in Set.
  • Comparable: Enables relational operators (<, >, <=, >=).
  • Codable: Provides out-of-the-box support for serialization and deserialization (e.g., JSON encoding).
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More