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

# Kotlin UInt

`UInt` is a value class in Kotlin that represents a 32-bit unsigned integer. It provides a numeric type strictly constrained to non-negative values, operating within a range of 0 to 4,294,967,295 ($2^{32} - 1$). At runtime, `UInt` wraps a standard primitive `Int`. While it is designed to avoid object allocation overhead where possible, it is subject to boxing depending on how it is used within the type system.

## Instantiation and Syntax

`UInt` literals are defined by appending the `u` or `U` suffix to an integer literal. The compiler can infer the type automatically, or it can be declared explicitly.

```kotlin theme={"dark"}
val inferredUInt = 42u               // Type inferred as UInt
val explicitUInt: UInt = 42U         // Explicit type declaration
val hexUInt = 0xFFFFFFFFu            // Hexadecimal literal
val binUInt = 0b10101010u            // Binary literal
```

## Type Conversion

Kotlin enforces strict type safety; implicit conversions between signed `Int` and unsigned `UInt` are prohibited. Explicit conversion functions must be invoked.

```kotlin theme={"dark"}
val signedInt: Int = 42
val unsignedInt: UInt = signedInt.toUInt()

val stringToUInt: UInt = "4294967295".toUInt()
val backToInt: Int = unsignedInt.toInt()
```

When converting a negative signed `Int` to a `UInt`, the bitwise representation remains identical, resulting in the two's complement unsigned equivalent.

```kotlin theme={"dark"}
val negativeInt: Int = -1
val wrappedUInt: UInt = negativeInt.toUInt() // Evaluates to 4294967295u (UInt.MAX_VALUE)
```

## Arithmetic and Overflow Semantics

`UInt` supports standard arithmetic and bitwise operators. Operations are executed with unsigned semantics, meaning division and modulo operations evaluate differently than their signed counterparts.

```kotlin theme={"dark"}
val a = 10u
val b = 3u
val division = a / b       // Evaluates to 3u
val remainder = a % b      // Evaluates to 1u
val bitwiseShift = a shl 2 // Evaluates to 40u
```

Like signed integers, `UInt` operations are subject to silent overflow and underflow, wrapping around the 32-bit boundary.

```kotlin theme={"dark"}
val max: UInt = UInt.MAX_VALUE
val overflow: UInt = max + 1u // Evaluates to 0u

val min: UInt = UInt.MIN_VALUE
val underflow: UInt = min - 1u // Evaluates to 4294967295u
```

## JVM Representation, Boxing, and Unboxing

Because the underlying Java Virtual Machine (JVM) lacks native support for unsigned integer types, Kotlin compiles `UInt` down to a standard Java primitive `int`. The Kotlin compiler handles the unsigned arithmetic by substituting the appropriate JVM intrinsics or library functions (e.g., `Integer.divideUnsigned`).

However, because `UInt` is a value class, it is subject to **boxing** and **unboxing**. Boxing occurs when the compiler is forced to instantiate an actual wrapper object on the heap instead of using the underlying primitive `int`. This introduces memory allocation and performance penalties. `UInt` is forced to box under the following conditions:

1. **Nullability:** When declared as a nullable type (`UInt?`).
2. **Generics:** When used as a generic type argument (e.g., `List<UInt>`).
3. **Upcasting:** When cast to `Any` or an interface.

```kotlin theme={"dark"}
val unboxed: UInt = 42u                    // Represented as primitive 'int' on the JVM (no allocation)
val boxedNullable: UInt? = 42u             // Boxed into a wrapper object (heap allocation)
val boxedGeneric: List<UInt> = listOf(42u) // Boxed due to generic type erasure
val boxedAny: Any = 42u                    // Boxed due to upcasting
```

## Name Mangling

To prevent signature clashes at the bytecode level—since both unboxed `Int` and `UInt` erase to `int`—the Kotlin compiler applies name mangling to functions accepting unboxed `UInt` parameters.

```kotlin theme={"dark"}
// Kotlin source
fun process(value: UInt) { }

// Decompiled Java bytecode equivalent
public static final void process-WZ4Q5Ns(int value) { }
```

## Arrays

To maintain performance and avoid the boxing overhead associated with generic arrays (like `Array<UInt>`), Kotlin provides `UIntArray`. This specialized array type is backed directly by a primitive `int[]` on the JVM but exposes `UInt` elements to the Kotlin type system, ensuring no wrapper objects are allocated for its elements.

```kotlin theme={"dark"}
val unsignedArray = UIntArray(5) { it.toUInt() } // Backed by int[], no boxing overhead
val literalArray = uintArrayOf(1u, 2u, 3u)       // Backed by int[], no boxing overhead

val boxedArray = Array<UInt>(3) { 0u }           // Avoid: incurs heap allocation for every element
```

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