简体   繁体   中英

How do I assign -0x80000000 to long long variable in VS2019?

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, ie 0x80000000LL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM