long int type
Hi every body!
I hope you guys are having a good day.
I was reading about long int type in c++. Here is what i find:
A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less,A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least 2147483647. Again, many 2's complement implementations will define LONG_MIN to be -2147483648 but this is not required.
An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at least 32 bits to hold the required range of values. LONG_MIN to be -2147483648 but this is not required.
An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at least 32 bits to hold the required range of values.
"long - min is required to be -21478364 or less",
my question is how much less? why long_min is required to be -214783647?
"A LONG_MAX must be at least 2147483647." why long-max must be atleast 2147483647?
Thanks a lot and have a nice day!
The point of the standard is to guarantee something. You know, no matter where you go (assuming a standard-compliant implementation) that a long int can handle that range. Some machines may give you a larger range, and a larger range would require a smaller number than -2147483648. The numbers were chosen as the range from LONG_MIN to LONG_MAX is a power of 2, specifically 2^32.
-2147483647 or less, not -214748364.
How much less? How about 9223372034707292161 less? That's a real result, taken from GCC running on a 64-bit Linux. There's no limit, really.
Why -2147483647? Because that's -(2^31 - 1), which is the smallest value a signed 32-bit value can take in a one's complement or sign/magnitude negative representation. The standard thus enforces this:
1) long (which is a synonym for long int) must be at least 32 bits wide (but may be wider).
2) One's complement and sign/magnitude are valid negative representations. So is two's complement, the most common representation these days.
And that large value I wrote above? That's the difference between -(2^63) (the lowest value a two's complement 64-bit integer can take) and -(2^31-1).
Those values imply that you are dealing with a number that is AT LEAST 32 bits. If we have a 36-bit processor, we can make long a 36-bit number. On a 64-bit machine that also supports 32-bit numbers, it can use EITHER 32 or 64-bit numbers - the spec allows the choice to be made by the implementation. In fact, in x86-64 for Linux, a long is 64-bit, and in x86-64 running Windows, long is 32-bit.
Thanks a lot guys for awesome responses!
have a nice day!