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 (). 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()(or0for uninitialized elements within aUShortArray)
Instantiation and Syntax
Because theu or U literal suffix defaults to UInt, instantiating a UShort requires an explicit type declaration or the use of conversion extension functions.
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.
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.
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.
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.
Master Kotlin with Deep Grasping Methodology!Learn More





