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

`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` ($2^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.

```swift theme={"dark"}
let decimal: UInt8 = 255
let hex: UInt8     = 0xFF
let octal: UInt8   = 0o377
let binary: UInt8  = 0b1111_1111
```

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

```swift theme={"dark"}
let sourceInt: Int = 300

// 1. Standard Initializer (Traps on overflow)
// let standard = UInt8(sourceInt) // Fatal error: Not enough bits to represent the passed value

// 2. Exact Initializer (Returns nil on overflow)
let exact = UInt8(exactly: sourceInt) // nil

// 3. Truncating Initializer (Performs bitwise truncation)
let truncated = UInt8(truncatingIfNeeded: sourceInt) // 44 (300 % 256)
```

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

```swift theme={"dark"}
let maxVal: UInt8 = 255
let minVal: UInt8 = 0

// Overflow Addition
let wrappedAdd = maxVal &+ 1 // 0

// Overflow Subtraction
let wrappedSub = minVal &- 1 // 255

// Overflow Multiplication
let wrappedMult = maxVal &* 2 // 254
```

## Bitwise Operations

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

```swift theme={"dark"}
let bits: UInt8 = 0b0000_1111 // 15

// Bitwise NOT
let inverted = ~bits          // 0b1111_0000 (240)

// Bitwise AND
let masked = bits & 0b0000_1010 // 0b0000_1010 (10)

// Bitwise Left Shift
let shifted = bits << 2       // 0b0011_1100 (60)
```

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

```swift theme={"dark"}
let int8Value: Int8 = -1
let bitPattern = UInt8(bitPattern: int8Value) // 255 (0xFF)
```

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