C++ Unsigned Short Maximum Value
In C++, an unsigned short
is an integer data type used to store only non-negative integer values. It uses 16 bits on most systems, which means it can represent values from 0 to 65,535. The maximum value of an unsigned short
is 65,535. This value is defined by the USHRT_MAX
macro in the <climits>
header.
Understanding Unsigned Short
The unsigned short
data type represents numbers in the range:
- Minimum Value: 0
- Maximum Value: 65,535
The range is derived from the formula:
0 to 2^n - 1
Where n
is the number of bits used by the data type. For an unsigned short
, n = 16
, resulting in:
0 to 2^16 - 1 = 0 to 65,535
C++ Program to Access Unsigned Short Maximum Value
You can programmatically access the maximum value of an unsigned short
using the USHRT_MAX
constant from the <climits>
header.
In the following example, we will demonstrate how to access and use USHRT_MAX
in a C++ program.
main.cpp
</>
Copy
#include <iostream>
#include <climits>
int main() {
// Accessing the maximum value of unsigned short
std::cout << "The maximum value of unsigned short is: " << USHRT_MAX << std::endl;
return 0;
}
Output
The maximum value of unsigned short is: 65535
Explanation
- The
<climits>
header provides macros for the limits of fundamental data types in C++. - The
USHRT_MAX
macro defines the maximum value of anunsigned short
, which is 65,535 for a 16-bit unsigned short. - The program uses
std::cout
to output the maximum value of anunsigned short
directly usingUSHRT_MAX
.