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

`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:

```swift theme={"dark"}
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:

```swift theme={"dark"}
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:

```swift theme={"dark"}
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) (`&+`, `&-`, `&*`):

```swift theme={"dark"}
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.

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