Cannot set INT_MIN to long

signed long long value = -2147483648;
    cout << ((signed long long)value);

outputs 2147483648 (no minus sign), why?

+5
source share
3 answers
signed long long value = -2147483648;

2147483648cannot be represented in a 32-bit integer value, so it is converted to unsigned, then the unary minus (which does not change anything) is applied, and then it is assigned a signed long long one. Use-2147483648LL

+8
source

A literal integer in C ++ is of type int. If it does not fit into this type, it can be interpreted as an unsigned integer. However, it is not guaranteed that it will automatically be considered as a larger integer type.

, , , .

-2147483648LL.

+1

undefined, . , 2147483648 ​​ 32- int. : " , . , , : int, long int; int, undefined. ++ 0x .

If LONG_MAX is greater than 2147483648, then the literal type is long, and the minus should give the correct value. Otherwise, if the compiler already supports long ones (and since then you have declared a variable of this type, you need to assume that it does), type 2147483648 is long, and minus should give the correct value. If the compiler does not support long and long - only 32 bits, then your code is undefined, so everything that the compiler does is “correct”.

0
source

All Articles