Skip to main content
UInt16 is a value type in Swift representing an unsigned 16-bit (2-byte) fixed-width integer. Because it is unsigned, it cannot represent negative numbers, allocating all 16 bits entirely to represent positive magnitudes and zero.

Value Range and Bounds

As a 16-bit unsigned integer, UInt16 has a strictly defined mathematical range:
  • Minimum Value: 0
  • Maximum Value: 65535 (21612^{16} - 1)
You can access these bounds programmatically using static properties:

Initialization and Literals

UInt16 can be initialized using standard base-10 integer literals, as well as hexadecimal, octal, and binary literals. Swift allows the use of underscores (_) for visual grouping.

Type Conversion

Swift does not implicitly convert between integer types. To assign a value from another integer type (like Int, UInt8, or UInt32) to a UInt16, you must perform an explicit initialization. If the source value exceeds the bounds of UInt16, a runtime trap occurs.

Overflow and Underflow Behavior

By default, Swift arithmetic operators (+, -, *) trap and crash the application if an operation exceeds the bounds of UInt16. To perform modulo arithmetic that wraps around the type’s boundaries, you must use Swift’s overflow operators (&+, &-, &*).

Endianness and Memory Layout

Because UInt16 occupies multiple bytes in memory, its byte order (endianness) is relevant. UInt16 provides properties to inspect and manipulate its byte representation relative to the host architecture.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More