CKTIS / C++
std::uint16_t — an unsigned integer with exactly 16 bits
std::uint16_t from <cstdint> is an optional alias for an unsigned integer type with exactly 16 bits and a range of 0–65535. It is useful when an exact width is required, but it does not guarantee a two-byte size on every platform.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <cstdint>
std::uint16_t value{42};std::uint16_t, available after #include <cstdint>, aliases an existing unsigned integer type rather than defining a distinct new type. It has exactly 16 bits, no padding bits, and represents values from 0 to 65535. Do not assume it always aliases unsigned short.
This alias is optional: an implementation provides it if it supports an integer type with the corresponding width and no padding bits. If at least 16 bits are sufficient, choose std::uint_least16_t. Exactly 16 bits does not necessarily mean sizeof(std::uint16_t) == 2, because sizeof measures C++ bytes, which need not contain 8 bits.
Converting an integer to std::uint16_t produces its value modulo 65536; therefore, static_cast<std::uint16_t>(65536UL) produces 0. This does not mean every operation on operands of this type uses 16-bit arithmetic: integral promotions may first change the operand types, often to int. Before calculations requiring a larger range, explicitly choose a suitably wider type.
The example shows the maximum value and the result of an explicit conversion. Casts to unsigned long ensure numeric output regardless of the alias's underlying type. The code is intended for C++23 compilation; selecting that mode alone does not attest to operation in a production runtime.
Examples
#include <cstdint>
#include <iostream>
int main() {
constexpr std::uint16_t maximum{65535};
constexpr auto wrapped = static_cast<std::uint16_t>(65536UL);
static_assert(wrapped == 0);
std::cout << static_cast<unsigned long>(maximum) << '\n'
<< static_cast<unsigned long>(wrapped) << '\n';
}
Result
65535 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.