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

`ULong` is an unsigned 64-bit integer type in Kotlin that represents values from 0 to 18,446,744,073,709,551,615 ($2^{64} - 1$). It is implemented as a value class (specifically, a `@JvmInline value class`) that wraps a standard signed 64-bit `Long`. This design provides unsigned arithmetic and bitwise semantics while avoiding object allocation overhead at runtime, compiling down to a primitive `long` on the JVM in unboxed contexts.

## Instantiation and Syntax

`ULong` literals are defined by appending the `u` (or `U`) and `L` suffixes to an integer literal.

```kotlin theme={"dark"}
val explicitULong: ULong = 18446744073709551615uL
val inferredULong = 123UL // Type inferred as ULong
val hexULong = 0xFFFFFFFFFFFFFFFFuL
val binULong = 0b10101010uL
```

Standard numeric types and strings can be explicitly converted to `ULong` using extension functions:

```kotlin theme={"dark"}
val fromInt: ULong = 42.toULong()
val fromSignedLong: ULong = (-1L).toULong() // Results in ULong.MAX_VALUE due to two's complement
val fromString: ULong = "18446744073709551615".toULong()
```

## Memory Representation and Boxing

Because `ULong` is an inline value class, the Kotlin compiler treats it as a primitive `long` during execution whenever possible. However, boxing occurs when `ULong` is used in generic contexts, nullable types, or as an interface implementation.

```kotlin theme={"dark"}
// Unboxed: Stored as a primitive 64-bit integer (long) on the JVM
val unboxed: ULong = 100UL 

// Boxed: Instantiates a wrapper object on the heap
val boxed: ULong? = 100UL 
val genericList: List<ULong> = listOf(100UL) 
```

## Operations and Semantics

`ULong` supports standard arithmetic (`+`, `-`, `*`, `/`, `%`) and comparison (`<`, `>`, `==`) operators. The Kotlin compiler translates these into unsigned operations at the bytecode level (e.g., utilizing `java.lang.Long.divideUnsigned` on the JVM).

```kotlin theme={"dark"}
val a = 10UL
val b = 3UL
val division = a / b // Evaluates to 3UL
```

**Bitwise Operations:**
`ULong` supports standard bitwise operations (`and`, `or`, `xor`, `inv`). However, bitwise shift semantics differ from signed `Long`:

* `shr` (Shift Right): Performs a **logical shift** (zero-fill), whereas `shr` on a signed `Long` performs an arithmetic shift (sign-extend).
* `ushr` (Unsigned Shift Right): Not available on `ULong`, as `shr` already provides the logical shift behavior.
* `shl` (Shift Left): Functions identically to signed integers.

```kotlin theme={"dark"}
val value = 0xFFFFFFFFFFFFFFFFuL
val shifted = value shr 8 // Fills highest 8 bits with 0s
```

## ULongArray

To prevent boxing overhead when working with collections of `ULong` data, Kotlin provides `ULongArray`. At the JVM level, `ULongArray` is backed by a primitive `long[]`, ensuring contiguous memory allocation and zero boxing per element.

```kotlin theme={"dark"}
// Initialization via factory function
val array = ULongArray(5) { index -> (index * 2).toULong() }

// Initialization via vararg function
val literalArray = ulongArrayOf(1UL, 2UL, 3UL)

// Access and mutation
array[0] = 99UL
val firstElement = array[0]
```

## Type Constraints and Interoperability

* **Widening Conversions:** Kotlin does not implicitly widen types. A `UInt` cannot be passed to a function expecting a `ULong` without an explicit `.toULong()` cast.
* **Java Interoperability:** Because Java lacks native unsigned types, `ULong` is exposed to Java code as a signed `long`. Functions accepting `ULong` in Kotlin will undergo name mangling in the generated JVM bytecode to prevent signature collisions with functions accepting signed `Long`.

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