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

`Short` is a 16-bit signed two's complement integer data type in Kotlin. It occupies 2 bytes of memory and represents numerical values strictly within the range of -32,768 to 32,767 ($-2^{15}$ to $2^{15}-1$).

## Memory and Bounds Constants

The `Short` companion object provides constant properties to access its structural limits:

* `Short.MIN_VALUE`: -32768
* `Short.MAX_VALUE`: 32767
* `Short.SIZE_BYTES`: 2
* `Short.SIZE_BITS`: 16

## Instantiation and Syntax

Kotlin does not provide a dedicated literal suffix for `Short` (unlike `L` for `Long` or `f` for `Float`). Because integer literals are inferred as `Int` by default, a `Short` must be instantiated via explicit type declaration or explicit conversion.

```kotlin theme={"dark"}
// Explicit type declaration (compiler implicitly narrows the Int literal)
val explicitShort: Short = 32000

// Explicit conversion function
val convertedShort: Short = 32000.toShort()

// Hexadecimal and binary literals
val hexShort: Short = 0x7FFF      // 32767
val binShort: Short = 0b01111111  // 127
```

## JVM Representation and Boxing

On the Kotlin/JVM platform, the compiler maps `Short` types based on nullability:

1. **Non-nullable (`Short`)**: Compiles directly to the Java primitive `short`.
2. **Nullable (`Short?`)**: Triggers boxing, compiling to the Java wrapper class `java.lang.Short`.

```kotlin theme={"dark"}
val primitive: Short = 100   // Represented as primitive 'short'
val boxed: Short? = 100      // Represented as 'java.lang.Short'
```

## Type Promotion in Arithmetic

Kotlin enforces strict type safety and does not implicitly widen types. When performing arithmetic operations involving `Short`, the compiler automatically promotes the operands to `Int` (or `Long` if the other operand is a `Long`) to prevent overflow during intermediate calculations.

Consequently, the return type of an arithmetic operation between two `Short` variables is an `Int`.

```kotlin theme={"dark"}
val a: Short = 10
val b: Short = 20

// 'result' is inferred as Int
val result = a + b 

// Explicit downcasting is required to assign the result back to a Short
val shortResult: Short = (a + b).toShort()
```

## Bitwise Operations

As of Kotlin 1.5, the standard library provides strict bitwise operations directly for the `Short` type. The infix functions `and`, `or`, and `xor`, as well as the unary function `inv()`, can be invoked directly on a `Short` and will return a `Short`.

However, bitwise shift operations (`shl`, `shr`, `ushr`) are not defined on the `Short` class. To perform bit shifts, the `Short` must first be explicitly promoted to an `Int`.

```kotlin theme={"dark"}
val a: Short = 0b0011
val b: Short = 0b0101

// Direct bitwise operations returning Short
val bitwiseAnd: Short = a and b
val bitwiseOr: Short = a or b
val bitwiseXor: Short = a xor b

// Unary bitwise inversion returning Short
val inverted: Short = a.inv() 

// Shift operations require promotion to Int
// Invalid: a shl 2 
val shifted: Int = a.toInt() shl 2
```

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