Range of int and long datatypes
I learned the range of int and long on 32 bit windows is:
signed int: -32767 to 32767
signed long: -2147483647 to 2147483647
why does the int has same range开发者_开发技巧 as long type as mentioned on msdn? http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx
Your premise is wrong. int
and long
are both 32 bit on (both) 32 and 64 bit Windows.
exact length is not mandated. What is mandated is that long cannot be smaller than int.
http://www.faqs.org/docs/learnc/x401.html
I assume you meant to write this:
signed int: -2147483647 to 2147483647
...
They are the same for entirely historical reasons. The C++ standard doesn't mandate a particular size for each type, the only guarantee between these two types is that sizeof(int) <= sizeof(long)
.
You int would compare to the __int16 whereas your long would compare to __int32.
You have to look at the Bytes column for this to make more sense.
When working with binary representations of integers you can calculate the maximum range of signed data types based on the number of bits used to represent the data using the formula:
lowest value: -2^(N-1) highest value: 2^(N-1) - 1
where N is the number of bits.
For one reason or another, the "signed int" type you're talking about is represented by 16 bits, and the "signed long" is represented by 32 bits.
Also notice that the ranges you posted seem to be incomplete, note that the range of a 16 bit signed int goes down to -32768, which is one lower than your posted range.
精彩评论