Skip to main content
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:

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.

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.

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.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More