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

`UByte` is an unsigned 8-bit integer type in Kotlin that represents values from 0 to 255, inclusive. It is implemented as an inline class (specifically, a `value class`) that wraps a standard signed 8-bit `Byte` to provide unsigned semantics without the runtime overhead of object allocation.

## Technical Specifications

* **Memory Footprint:** 8 bits (1 byte)
* **Minimum Value:** `UByte.MIN_VALUE` (0)
* **Maximum Value:** `UByte.MAX_VALUE` (255)
* **Type Hierarchy:** Implements `Comparable<UByte>`

## Instantiation and Syntax

`UByte` instances are created using the `u` or `U` literal suffix, or via explicit conversion functions from other numeric types.

```kotlin theme={"dark"}
// Literal assignment using the 'u' or 'U' suffix
val max: UByte = 255u
val hex: UByte = 0xFFu
val bin: UByte = 0b1111_1111u

// Explicit conversion from signed types
val fromInt: UByte = 128.toUByte()
val fromByte: UByte = (-1).toByte().toUByte() // Results in 255u
```

## JVM Representation and Boxing

On the JVM, `UByte` is compiled down to a primitive `byte`. The Kotlin compiler enforces unsigned constraints at compile time and emits the necessary bytecode (such as bitwise masking) to simulate unsigned operations.

Boxing (allocating a heap object) only occurs when `UByte` is used in contexts requiring an object reference:

1. When declared as a nullable type (`UByte?`).
2. When used as a generic type argument (e.g., `List<UByte>`).

## Operations and Type Promotion

Standard arithmetic operations are supported. However, to prevent silent overflows, arithmetic operations (`+`, `-`, `*`, `/`, `%`) between two `UByte` values automatically promote the result to a `UInt`.

Bitwise operations (`and`, `or`, `xor`) and bitwise shift operators (`shl`, `shr`) are not defined directly on `UByte`. To perform these operations, the value must be explicitly converted to a larger unsigned type (`UInt` or `ULong`) first.

Kotlin does **not** define the `ushr` (unsigned shift right) operator for any unsigned types. Because unsigned types inherently do not sign-extend, the standard `shr` operator already performs a logical (unsigned) shift, making `ushr` unnecessary. The bitwise inversion function (`inv()`) is the only bitwise operation supported directly on `UByte`.

```kotlin theme={"dark"}
val a: UByte = 200u
val b: UByte = 100u

// The result of a + b is promoted to UInt
val sum: UInt = a + b 

// Explicit downcasting is required to assign the result back to UByte
val truncatedSum: UByte = (a + b).toUByte() // Results in 44u (300 % 256)

// Bitwise inversion is supported directly
val inverted: UByte = a.inv()

// Bitwise AND, OR, XOR, and shifts require explicit conversion to UInt or ULong
val andResult: UByte = (a.toUInt() and b.toUInt()).toUByte()

// shr performs a logical shift (0-fill) on unsigned types; ushr is not available
val shifted: UInt = a.toUInt() shr 2 
```

## Arrays

Kotlin provides `UByteArray` to represent an array of unboxed unsigned bytes. Under the hood, `UByteArray` is an inline class wrapping a standard `ByteArray`, ensuring that no boxing occurs for the elements within the array.

```kotlin theme={"dark"}
// Creating an unboxed array of 10 UByte elements initialized to 0u
val buffer = UByteArray(10)

// Creating and initializing via a factory function
val initializedBuffer = UByteArray(5) { index -> index.toUByte() }

// Literal array creation
val literalArray = ubyteArrayOf(0x01u, 0x02u, 0x03u)
```

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