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.

UShort is an unsigned 16-bit integer data type in Kotlin that represents values from 0 to 65,535 (21612^{16} - 1). It is implemented as a value class that wraps a standard signed Short, providing unsigned semantics at compile time while avoiding object allocation overhead at runtime.

Technical Specifications

  • Memory Size: 16 bits (2 bytes)
  • Minimum Value: UShort.MIN_VALUE (0)
  • Maximum Value: UShort.MAX_VALUE (65,535)
  • Default Value: 0.toUShort() (or 0 for uninitialized elements within a UShortArray)

Instantiation and Syntax

Because the u or U literal suffix defaults to UInt, instantiating a UShort requires an explicit type declaration or the use of conversion extension functions.
// Explicit type declaration with unsigned literal suffix
val explicitUShort: UShort = 42u

// Conversion from standard signed integer
val convertedUShort: UShort = 65000.toUShort()

// Hexadecimal and binary literals
val hexUShort: UShort = 0xFFFFu
val binUShort: UShort = 0b10101010u.toUShort()

Type Promotion in Arithmetic Operations

When performing standard arithmetic operations (+, -, *, /, %) on UShort variables, Kotlin automatically promotes the operands to UInt to prevent silent overflow. The result must be explicitly cast back to UShort if a 16-bit type is required.
val a: UShort = 10u
val b: UShort = 20u

// The result of (a + b) is promoted to UInt
val sumAsUInt: UInt = a + b 

// Explicit downcast required to assign back to UShort
val sumAsUShort: UShort = (a + b).toUShort()

Bitwise Operations

Unlike arithmetic operators, bitwise infix functions (and, or, xor, shl, shr, ushr) are not defined for UShort. Kotlin does not automatically promote UShort operands for bitwise operations. To perform bitwise manipulation, the operands must be explicitly converted to UInt or ULong first.
val a: UShort = 0x00FFu
val b: UShort = 0xFF00u

// Explicit conversion to UInt is required before bitwise operations
val bitwiseOr: UInt = a.toUInt() or b.toUInt()

// Convert back to UShort if necessary
val bitwiseOrAsUShort: UShort = (a.toUInt() or b.toUInt()).toUShort()

Underflow and Overflow Semantics

UShort adheres to standard two’s complement arithmetic during conversions. Converting a negative signed Short or Int to UShort maps the underlying binary representation directly to its unsigned equivalent.
val negativeInt: Int = -1
// -1 in two's complement becomes the maximum UShort value
val underflow: UShort = negativeInt.toUShort() // 65535u

val max: UShort = UShort.MAX_VALUE
// Overflow wraps around to 0
val overflow: UShort = (max + 1u).toUShort() // 0u

JVM Representation and Arrays

On the JVM, UShort is compiled down to the primitive short type. The Kotlin compiler handles the unsigned arithmetic via intrinsic functions. However, using UShort as a generic type parameter (e.g., List<UShort>) forces boxing, allocating an object wrapper on the heap. To maintain primitive performance and avoid boxing overhead for collections of UShort, Kotlin provides the specialized UShortArray class, which is backed by a primitive short[] at the JVM level.
// Boxed representation (Heap allocation per element)
val boxedList: List<UShort> = listOf(1u, 2u, 3u)

// Unboxed primitive array (Contiguous memory, no boxing)
val unboxedArray: UShortArray = ushortArrayOf(1u, 2u, 3u)

// Initializing a UShortArray with a size and lambda
val dynamicArray = UShortArray(5) { index -> (index * 2).toUShort() }
Master Kotlin with Deep Grasping Methodology!Learn More