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 forunsigned 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).
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 strictly0. The maximum value is , where 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
Theint 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 anunsigned 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 onunsigned short are promoted to int, the arithmetic itself does not typically overflow. Instead, modulo 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.
Master C++ with Deep Grasping Methodology!Learn More





