Skip to main content
UInt8 is a value type in Swift representing an 8-bit unsigned integer. It occupies exactly one byte of memory and stores non-negative whole numbers. As a fixed-width integer, its memory footprint is deterministic regardless of the underlying hardware architecture.

Technical Specifications

  • Memory Size: 8 bits (1 byte)
  • Minimum Value (UInt8.min): 0
  • Maximum Value (UInt8.max): 255 (2812^8 - 1)
  • Protocol Conformances: UnsignedInteger, FixedWidthInteger, BinaryInteger, Hashable, Codable, Sendable.

Initialization and Literals

UInt8 can be initialized using standard decimal integers, as well as hexadecimal, octal, and binary literals.

Type Conversion

Swift does not implicitly convert between integer types. Converting another integer type to UInt8 requires explicit initialization. Because UInt8 has a narrow bounds range, Swift provides different initializers to handle potential out-of-bounds conversions:

Arithmetic and Overflow Behavior

By default, standard arithmetic operators (+, -, *) in Swift trap (crash) if the operation results in a value outside the 0...255 range. To perform modulo arithmetic that wraps around the bounds, UInt8 utilizes Swift’s overflow operators (&+, &-, &*).

Bitwise Operations

Because UInt8 conforms to FixedWidthInteger, it fully supports bitwise manipulation. Operations are applied directly to the 8-bit underlying binary representation.

Memory Representation

You can access the raw byte representation of a UInt8 or convert raw bytes into a UInt8 using its bitPattern initializers, which is particularly relevant when interfacing with C APIs or unsafe memory pointers.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More