I am using Visual Studio 2019 Community edition. I have the following C++ code snippet:
long long l = 0x80000000;
assert(l > 0); //true
l = -0x7fffffff;
assert(l < 0); // true
l = -0x80000000;
assert(l < 0); // FALSE
I expect -0x80000000
to end up sign-extended to 64-bit value of 0xFFFFFFFF80000000 but it doesn't. Instead it carries the positive 0x80000000 value. What do I miss?
There are rules for integral literals without suffixes.
In short, 0x80000000
's type is unsigned int
because it's an hexadecimal number bigger than int
's max value. In order to make it long long, you must add the LL
suffix, i.e. 0x80000000LL
.
User contributions licensed under CC BY-SA 3.0