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

# C++ unsigned short

`unsigned short` (formally `unsigned short int`) is a fundamental integral data type in C++ designed to store non-negative whole numbers. By applying the `unsigned` specifier, the sign bit used in standard signed integers is repurposed as a magnitude bit, shifting the representable range entirely to zero and positive values.

## Memory Size and Standard Guarantees

The C++ standard does not dictate an exact byte size for `unsigned short`, but it enforces the following constraints:

* It must be at least 16 bits wide.
* It must satisfy the size hierarchy: `sizeof(unsigned char) <= sizeof(unsigned short) <= sizeof(unsigned int)`.

While `unsigned short` is commonly 2 bytes on systems where a byte (`CHAR_BIT`) is 8 bits, it is not guaranteed to be at least 2 bytes. On architectures where `CHAR_BIT` is 16 or larger (such as certain DSPs), `sizeof(unsigned short)` can legally evaluate to `1`.

## Value Range

Because it lacks a sign bit, the minimum value is strictly `0`. The maximum value is $2^N - 1$, where $N$ is the number of bits. For a standard 16-bit implementation, the range is **0 to 65,535**.

You can query these limits programmatically using the `<limits>` header:

```cpp theme={"dark"}
#include <iostream>
#include <limits>

int main() {
    std::cout << "Size: " << sizeof(unsigned short) << " bytes\n";
    std::cout << "Min: " << std::numeric_limits<unsigned short>::min() << "\n"; // Always 0
    std::cout << "Max: " << std::numeric_limits<unsigned short>::max() << "\n"; // Typically 65535
    return 0;
}
```

## Syntax and Initialization

The `int` keyword is optional when declaring an `unsigned short`. C++ does not possess a specific literal suffix for `short` types (like `u` for `unsigned int`). Integer literals are implicitly converted to `unsigned short` upon assignment.

```cpp theme={"dark"}
unsigned short val1 = 42;         // Standard declaration
unsigned short int val2 = 65000;  // Equivalent, fully qualified declaration
unsigned short val3{100};         // Uniform initialization (prevents narrowing conversions)
```

## Integral Promotion

When an `unsigned short` is used in arithmetic or bitwise operations, it is subject to **integral promotion**. Before the operation is evaluated, the compiler promotes the `unsigned short` operands to standard `int` (provided `int` is large enough to represent all possible values of `unsigned short`, which is true on standard 32-bit and 64-bit systems). If `int` cannot hold the values, they are promoted to `unsigned int`.

```cpp theme={"dark"}
unsigned short a = 10;
unsigned short b = 20;

// 'a' and 'b' are promoted to 'int' before addition.
// The type of (a + b) is 'int'.
auto c = a + b; 
```

## Conversion and Wrap-Around Behavior

Because arithmetic operations on `unsigned short` are promoted to `int`, the arithmetic itself does not typically overflow. Instead, modulo $2^N$ wrap-around behavior occurs during the implicit integer conversion when an out-of-range `int` result is assigned back to an `unsigned short` variable.

Unlike signed integer overflow, which invokes undefined behavior (UB), out-of-range assignment to an unsigned type is strictly defined by the C++ standard to reduce modulo the number of representable values.

* **Overflow on Assignment:** Assigning a value greater than the maximum wraps around starting from `0`.
* **Underflow on Assignment:** Assigning a negative value wraps around to the upper end of the representable range.

```cpp theme={"dark"}
unsigned short max_val = 65535;

// max_val + 1 evaluates to 65536 of type 'int' (no overflow).
// Assigning 65536 back to unsigned short wraps around to 0.
unsigned short overflow_val = max_val + 1; 

unsigned short min_val = 0;

// min_val - 1 evaluates to -1 of type 'int'.
// Assigning -1 back to unsigned short wraps around to 65535.
unsigned short underflow_val = min_val - 1; 
```

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