Sunday, 8 September 2013

C++ Why does LLONG_MIN == -LLONG_MIN

C++ Why does LLONG_MIN == -LLONG_MIN

In C++ if I do this:
__int64 var = LLONG_MIN;
__int64 var2 = -var;
cout << "var: "<< var << endl;
cout << "var2: "<< var2 << endl;
The output I get is:
var: -9223372036854775808
var2: -9223372036854775808
What is the part of the standard that covers this? I assume it's signed
integer overflow. This was compiled using g++ (GCC) 4.7.2.
I have a subtract function and I'm writing an add function and I thought I
could just do this: add( someobj &obj, long long num ) { subtract( obj,
-num ); }. I think that would work if it wasn't for LLONG_MIN.

No comments:

Post a Comment