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.

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 2N12^N - 1, where NN 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:
#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.
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.
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 2N2^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.
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; 
Master C++ with Deep Grasping Methodology!Learn More