0

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?

Kok How Teh
  • 3,298
  • 6
  • 47
  • 85

1 Answers1

2

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.

Joel Filho
  • 1,300
  • 1
  • 5
  • 7
  • 1
    Correct for Visual Studio. Other implementations could have other maximum values for `int`, but 32 bits is by far the most common size today. – MSalters Oct 26 '20 at 09:31