Skip to main content

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.

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.
// 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.
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.
// 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)
Master Kotlin with Deep Grasping Methodology!Learn More