So, what's wrong with the previous version of my code?
Presumably, you're using a pre-2011 compiler, and on your system long
has 32 bits. The value (-231) isn't guaranteed to fit into long
, so it might overflow. That gives undefined behaviour, so you could see anything.
The most likely explanation for the particular value you see (231) is that, in the absence of defined behaviour in C++, your compiler is using the old C90 rules, and converting the value to unsigned long
.
Istn't it the type int?
Before 2011, it was int
if the value is representable by int
, otherwise long
, with undefined behaviour if that isn't sufficient. C++11 adds the long long
type, and allows that to be used for integer literals if long
isn't big enough.
or what the lower bound the Integer is exactly?
Signed integer types with N bits have a range of at least -2(N-1)+1 to 2(N-1)-1. Your value is -231, which is just out of range for a 32-bit signed type.
The language doesn't specify the exact size of the integer types; just that int
must have at least 16 bits, long
at least 32, and (since 2011) long long
at least 64.