CKTIS / C++
unsigned short — unsigned short integer type
unsigned short is a built-in unsigned integer type, equivalent to unsigned short int. It supports at least the values from 0 through 65535, but need not be exactly 16 bits wide. In calculations, it is subject to integral promotions.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
unsigned short name{value};unsigned short and unsigned short int name the same built-in unsigned integer type, distinct from short. The declaration unsigned short count{42}; creates a variable with the value 42; the type itself requires no header.
For width N, the range is 0 through 2^N − 1, with N at least 16. The exact width is implementation-defined: do not assume exactly 16 bits or sizeof(unsigned short) == 2. Obtain the maximum using std::numeric_limits<unsigned short>::max() from <limits>.
In many expressions, an unsigned short value is first promoted to int if that type can represent its entire range; otherwise, it is promoted to unsigned int. Operations on these variables therefore need not be performed in unsigned short. Unsigned arithmetic is modular, but that does not prevent signed overflow in an expression after promotion.
Converting an integer to unsigned short produces the congruent value modulo 2^N. Thus, static_cast<unsigned short>(-1) produces the maximum, not an error or a negative value. Check the range when wrapping is unwanted. The initialization unsigned short value{-1}; is ill-formed because it requires a narrowing conversion.
The example prints 42 and true regardless of the type's exact width. This entry targets C++23; the supplied site hosts an evolving standard draft, not a frozen C++23 text.
Examples
#include <iostream>
#include <limits>
int main() {
unsigned short count{42};
unsigned short wrapped = static_cast<unsigned short>(-1);
std::cout << count << '\n';
std::cout << std::boolalpha
<< (wrapped == std::numeric_limits<unsigned short>::max())
<< '\n';
}
Result
42 true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.