> ## 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.

# Swift UInt16

`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` ($2^{16} - 1$)

You can access these bounds programmatically using static properties:

```swift theme={"dark"}
let lowestValue = UInt16.min  // 0
let highestValue = UInt16.max // 65535
let width = UInt16.bitWidth   // 16
```

## 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.

```swift theme={"dark"}
let decimal: UInt16 = 65000
let hex: UInt16 = 0xFEF8
let octal: UInt16 = 0o176570
let binary: UInt16 = 0b1111_1101_1110_1000
```

## 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.

```swift theme={"dark"}
let eightBit: UInt8 = 250
let sixteenBit = UInt16(eightBit) // Explicit promotion to UInt16

let largeInt: Int = 70000
// let invalid = UInt16(largeInt) // Fatal error: Not enough bits to represent the passed value

let exactConversion = UInt16(exactly: largeInt) // Returns nil instead of crashing
```

## 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 (`&+`, `&-`, `&*`).

```swift theme={"dark"}
var maxVal = UInt16.max // 65535

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

// Overflow addition wraps to the minimum value
let wrappedAdd = maxVal &+ 1 // 0

var minVal = UInt16.min // 0

// Overflow subtraction wraps to the maximum value
let wrappedSub = minVal &- 1 // 65535
```

## 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.

```swift theme={"dark"}
let value: UInt16 = 0x1234

// Swaps the high-order and low-order bytes
let swapped = value.byteSwapped // 0x3412

// Accessing specific endian representations
let bigEndianValue = value.bigEndian
let littleEndianValue = value.littleEndian
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
