tags:

views:

390

answers:

9

Can unsigned long int hold a ten digits number (1,000,000,000 - 9,999,999,999) on a 32-bit computer.
What is the what is the range of unsigned long int , long int, unsigned int, short int, short unsigned int and int?

+1  A: 

You should look at the specialisations of the numeric_limits<> template for a given type. Its in the header.

Visage
+3  A: 

Have a look at this reference: http://www.cppreference.com/wiki/data%5Ftypes

jldupont
That reference is not correct - in particular it is not required that the range of `signed char` include -128, or the range of `short` and `int` include -32768 (and so on). It is also not true that `int` must be either 2 or 4 bytes - a 24 bit `int` type is certainly allowed.
caf
It's especially untrue that `long int` must be 4 bytes. It's 8 on 64bit linux, which is not some obscure platform you'll never encounter. That first link is just bogus, the author either hasn't read the standard or else is lying to keep things simple for noobs.
Steve Jessop
I've pruned the first reference from the list: to be honest, I just pasted-in the first googled result... I usually don't do this but it was early in the morning ;-)
jldupont
+8  A: 

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 vale will be in the form of 2n-1 because of how the numbers of represent in memory. Take for example two bits (8 bytes)

  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.

Yacoby
Minimum sizes for the integer type are mandated by the relevant standards (although exact sizes are not).
caf
+1: was completely unaware of numeric_limits. Thanks :)
Binary Worrier
+1  A: 

You might want to look at a table of data types, like so:

http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx

Amber
A: 

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

No

Justin
+1  A: 

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%28VS.80%29.aspx

Raymond
+6  A: 

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, the minimum range for the C99 type long long int, if your compiler supplies 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).

caf
+2  A: 

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 app that will do the following.

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

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

Lets assume it's 4.

You now want the maximum value 4 bytes can store, the max value for one byte is (in hex)0xFF. The max value of four bytes is 0x followed by 8 f's (one pair of f's for each byte, 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;

Thats the max 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).

Hope this helps

Binary Worrier
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
A: 

For unsigned data type there is no sign bit and all bits are for data ; whereas for signed data type MSB is indicated sign bit and remaining bits are for data.

To find the range do following things :

Step:1 -> Find out no of bytes for the give data type.

Step:2 -> Apply following calculations.

      Let n = no 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 e.g.

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]

Ashish