95

Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer?

Additionally, what are the ranges of unsigned long int , long int, unsigned int, short int, short unsigned int, and int?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yihangho
  • 2,185
  • 4
  • 25
  • 37

11 Answers11

157

The minimum ranges you can rely on are:

  • short int and int: -32,767 to 32,767
  • unsigned short int and unsigned int: 0 to 65,535
  • long int: -2,147,483,647 to 2,147,483,647
  • unsigned long int: 0 to 4,294,967,295

This means that no, long int cannot be relied upon to store any 10-digit number. However, a larger type, long long int, was introduced to C in C99 and C++ in C++11 (this type is also often supported as an extension by compilers built for older standards that did not include it). The minimum range for this type, if your compiler supports it, is:

  • long long int: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
  • unsigned long long int: 0 to 18,446,744,073,709,551,615

So that type will be big enough (again, if you have it available).


A note for those who believe I've made a mistake with these lower bounds: the C requirements for the ranges are written to allow for ones' complement or sign-magnitude integer representations, where the lowest representable value and the highest representable value differ only in sign. It is also allowed to have a two's complement representation where the value with sign bit 1 and all value bits 0 is a trap representation rather than a legal value. In other words, int is not required to be able to represent the value -32,768.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
caf
  • 233,326
  • 40
  • 323
  • 462
35

The size of the numerical types is not defined in the C++ standard, although the minimum sizes are. The way to tell what size they are on your platform is to use numeric limits

For example, the maximum value for a int can be found by:

std::numeric_limits<int>::max();

Computers don't work in base 10, which means that the maximum value will be in the form of 2n-1 because of how the numbers of represent in memory. Take for example eight bits (1 byte)

  0100 1000

The right most bit (number) when set to 1 represents 20, the next bit 21, then 22 and so on until we get to the left most bit which if the number is unsigned represents 27.

So the number represents 26 + 23 = 64 + 8 = 72, because the 4th bit from the right and the 7th bit right the left are set.

If we set all values to 1:

11111111

The number is now (assuming unsigned)
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 28 - 1
And as we can see, that is the largest possible value that can be represented with 8 bits.

On my machine and int and a long are the same, each able to hold between -231 to 231 - 1. In my experience the most common size on modern 32 bit desktop machine.

shriek
  • 5,605
  • 8
  • 46
  • 75
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • 1
    Minimum sizes for the integer type are mandated by the relevant standards (although exact sizes are not). – caf Nov 30 '09 at 11:22
16

To find out the limits on your system:

#include <iostream>
#include <limits>
int main(int, char **) {
  std::cout
    << static_cast< int >(std::numeric_limits< char >::max()) << "\n"
    << static_cast< int >(std::numeric_limits< unsigned char >::max()) << "\n"
    << std::numeric_limits< short >::max() << "\n"
    << std::numeric_limits< unsigned short >::max() << "\n"
    << std::numeric_limits< int >::max() << "\n"
    << std::numeric_limits< unsigned int >::max() << "\n"
    << std::numeric_limits< long >::max() << "\n"
    << std::numeric_limits< unsigned long >::max() << "\n"
    << std::numeric_limits< long long >::max() << "\n"
    << std::numeric_limits< unsigned long long >::max() << "\n";
}

Note that long long is only legal in C99 and in C++11.

Hal Canary
  • 2,154
  • 17
  • 17
11

Other folks here will post links to data_sizes and precisions, etc. I'm going to tell you how to figure it out yourself. Write a small application that will do the following.

unsigned int ui;
std::cout << sizeof(ui));

This will (depending on compiler and architecture) print 2, 4 or 8, saying 2 bytes long, 4 bytes long, etc.

Let’s assume it's 4.

You now want the maximum value 4 bytes can store, the maximum value for one byte is (in hexadecimal) 0xFF. The maximum value of four bytes is 0x followed by 8 f's (one pair of f's for each byte, and the 0x tells the compiler that the following string is a hex number). Now change your program to assign that value and print the result:

unsigned int ui = 0xFFFFFFFF;
std::cout << ui;

that’s the maximum value an unsigned int can hold, shown in base 10 representation.

Now do that for long's, shorts and any other INTEGER value you're curious about.

NB: This approach will not work for floating point numbers (i.e. double or float).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • 2
    If you try this with signed ints, you get negative numbers. Read up on "two's compliment" (link provided), it's easy to get the full range (positive and negative) for these too. http://en.wikipedia.org/wiki/Twos_Compliment – Binary Worrier Nov 30 '09 at 11:34
9

In C++, now int and other data is stored using the two's complement method.

That means the range is:

-2147483648 to 2147483647

or -2^31 to 2^31-1.

1 bit is reserved for 0 so positive value is one less than 2^(31).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shah Rukh Qasim
  • 322
  • 2
  • 6
7

You can use the numeric_limits<data_type>::min() and numeric_limits<data_type>::max() functions present in limits header file and find the limits of each data type.

#include <iostream>
#include <limits>
using namespace std;
int main()
{
    cout<<"Limits of Data types:\n";    
    cout<<"char\t\t\t: "<<static_cast<int>(numeric_limits<char>::min())<<" to "<<static_cast<int>(numeric_limits<char>::max())<<endl;
    cout<<"unsigned char\t\t: "<<static_cast<int>(numeric_limits<unsigned char>::min())<<" to "<<static_cast<int>(numeric_limits<unsigned char>::max())<<endl;
    cout<<"short\t\t\t: "<<numeric_limits<short>::min()<<" to "<<numeric_limits<short>::max()<<endl;
    cout<<"unsigned short\t\t: "<<numeric_limits<unsigned short>::min()<<" to "<<numeric_limits<unsigned short>::max()<<endl;
    cout<<"int\t\t\t: "<<numeric_limits<int>::min()<<" to "<<numeric_limits<int>::max()<<endl;
    cout<<"unsigned int\t\t: "<<numeric_limits<unsigned int>::min()<<" to "<<numeric_limits<unsigned int>::max()<<endl;
    cout<<"long\t\t\t: "<<numeric_limits<long>::min()<<" to "<<numeric_limits<long>::max()<<endl;
    cout<<"unsigned long\t\t: "<<numeric_limits<unsigned long>::min()<<" to "<<numeric_limits<unsigned long>::max()<<endl;
    cout<<"long long\t\t: "<<numeric_limits<long long>::min()<<" to "<<numeric_limits<long long>::max()<<endl;
    cout<<"unsiged long long\t: "<<numeric_limits<unsigned long long>::min()<<" to "<<numeric_limits<unsigned long long>::max()<<endl;
    cout<<"float\t\t\t: "<<numeric_limits<float>::min()<<" to "<<numeric_limits<float>::max()<<endl;
    cout<<"double\t\t\t: "<<numeric_limits<double>::min()<<" to "<<numeric_limits<double>::max()<<endl;
    cout<<"long double\t\t: "<<numeric_limits<long double>::min()<<" to "<<numeric_limits<long double>::max()<<endl;
}

The output will be: Limits of Data types:

  • char : -128 to 127
  • unsigned char : 0 to 255
  • short : -32768 to 32767
  • unsigned short : 0 to 65535
  • int : -2147483648 to 2147483647
  • unsigned int : 0 to 4294967295
  • long : -2147483648 to 2147483647
  • unsigned long : 0 to 4294967295
  • long long : -9223372036854775808 to 9223372036854775807
  • unsigned long long : 0 to 18446744073709551615
  • float : 1.17549e-038 to 3.40282e+038
  • double : 2.22507e-308 to 1.79769e+308
  • long double : 3.3621e-4932 to 1.18973e+4932
duplex143
  • 619
  • 2
  • 9
  • 25
insearchofcode
  • 438
  • 5
  • 9
  • The (internal) Markdown formatting is quite far from how it is rendered (e.g., vertical alignment). An alternative way could be to reformat it as a [Markdown table](https://meta.stackexchange.com/questions/356997/new-feature-table-support). – Peter Mortensen Sep 12 '22 at 17:15
  • But the OP may not be coming back - *"Last seen more than a month ago"*. Somebody could take the decision (and do it). – Peter Mortensen Sep 12 '22 at 17:18
3

For an unsigned data type, there isn't any sign bit and all bits are for data ; whereas for a signed data type, MSB is indicating a sign bit and the remaining bits are for data.

To find the range, do the following things:

Step 1: Find out number of bytes for the given data type.

Step 2: Apply the following calculations.

      Let n = number of bits in data type

      For signed data type ::
            Lower Range = -(2^(n-1))
            Upper Range = (2^(n-1)) - 1)

      For unsigned data type ::
            Lower Range = 0
            Upper Range = (2^(n)) - 1

For example,

For unsigned int size = 4 bytes (32 bits) → Range [0, (2^(32)) - 1]

For signed int size = 4 bytes (32 bits) → Range [-(2^(32-1)), (2^(32-1)) - 1]

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashish
  • 8,441
  • 12
  • 55
  • 92
1

No, only part of ten digits number can be stored in a unsigned long int whose valid range is 0 to 4,294,967,295.

You can refer to this: http://msdn.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raymond
  • 744
  • 5
  • 12
0

You should look at the specialisations of the numeric_limits<> template for a given type. It’s in the <limits> header.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
0

Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer.

No

justin
  • 104,054
  • 14
  • 179
  • 226
0

When starting to learn about different data types, I found it confusing due to the theoretical approach which made everything seem complicated. However, after trying out a practical example on my own, things became much clearer.

  • For instance, in the DECIMAL system, the largest number with 8 digits is 99999999.

  • Similarly, in the BINARY system, the largest number with 8 bits would be 11111111.

However, the size of the "int" data type can vary depending on the system's architecture, like 2 bytes (16-bit) or 4 bytes (32-bit). To determine the exact size, we use the "sizeof()" function.

For example, on a 32-bit architecture, "sizeof(int)" is 4 bytes, which is equivalent to 32 bits. To represent the largest value of an "int 4 bytes" on this architecture, you would need 32 ones, meaning (2 x 2^31) - 1 = 4294967295 for the "unsigned long int" data type. (You would need to write your own binary to decimal conversion program in any language without using pre-defined library or method to understand this more, trust me.)