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

`UShort` is an unsigned 16-bit integer data type in Kotlin that represents values from 0 to 65,535 ($2^{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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
// 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() }
```

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