Open In App

Maximum value of unsigned short int in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Being an unsigned data type, it can store only positive values.
  2. Takes a size of 16 bits.
  3. 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).
  4. 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.
  5. The minimum value that can be stored in unsigned short int is zero.
  6. 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++:

C++




// C++ program to obtain the maximum
// value that we can store in an
// unsigned short int
#include <climits>
#include <iostream>
using namespace std;
  
int main()
{
    // From the constant of climits
    // header file
    unsigned short int valueFromLimits = USHRT_MAX;
    cout << "Value from climits "
         << "constant: "
         << valueFromLimits << "\n";
  
    // using the wrap around property
    // of data types
  
    // Initialize variable with value 0
    unsigned short int value = 0;
  
    // subtract 1 from the value since
    // unsigned data type cannot store
    // negative number, the value will
    // wrap around and store the maximum
    // value that can store in it
    value = value - 1;
  
    cout << "Value using the wrap "
         << "around property: "
         << value << "\n";
  
    return 0;
}


Output:

Value from climits constant: 65535
Value using the wrap around property: 65535


Last Updated : 28 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads