Maximum value of unsigned short int in C++
In this article, we will discuss the unsigned short int data type in C++. It is the smallest (16 bit) integer data type in C++.
Some properties of the unsigned short int data type are:
- Being an unsigned data type, it can store only positive values.
- Takes a size of 16 bits.
- A maximum integer value that can be stored in an unsigned short int data type is typically 65535, around 216 – 1(but is compiler dependent).
- The maximum value that can be stored in unsigned short int is stored as a constant in <climits> header file whose value can be used as USHRT_MAX.
- The minimum value that can be stored in unsigned short int is zero.
- In case of overflow or underflow of data type, the value is wrapped around. For example, if 0 is stored in an unsigned short int data type and 1 is subtracted from it, the value in that variable will become equal to 65535. Similarly, in the case of overflow, the value will round back to zero.
Below is the program to get the highest value that can be stored in unsigned short int in C++:
valueFromLimits = USHRT_MAX;
"Value from climits "
<< valueFromLimits <<
value = 0;
value = value - 1;
"Value using the wrap "
"around property: "
<< value <<
Value from climits constant: 65535
Value using the wrap around property: 65535
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!