Int_min writes (-2147483647-1) and vs in Warning C4018: ">": An in-depth analysis of signed/unsigned mismatches.

Source: Internet
Author: User

The first clear 4 byte int can represent the range of numbers is 2147483647 ——-2147483648, to see the problem I encountered:


The "equality" description is output here.

(INT_MIN==-2147483648)

Is true.
Output "1<-2147483648", stating that

(1<-2147483648)

Is true, but this is unscientific.
Output " -1>2147483648" description

( -1>2147483648)

For true, this is not scientific either.
Int_min and-2147483648 are equal, what is the above output going on? Take a look at how int_min is defined, in Limits.h.

  #define INT_MIN     (-2147483647 - 1)

From here we can see some clues, why not write directly-2147483648 but write-2147483647-1? The original C + + standard-2147483648 is not an integral type of constant. In C + +, an integer constant begins with a number, without a decimal point or exponent, which may contain a prefix that represents a binary or a suffix of a representation type. The original text reads as follows:

“An integer constant begins with a digit, but has no period or exponent part. It may have a prefix that specifies its base and a suffix that specifies its type.”

There is no mention of symbols in this, so 2147483648 is actually a constant expression. A constant expression consisting of a '-' and integral-type constant 2147483648. Since I still use vs2012 to stay in the C89 standard, C89 matches the shaping constants in the following order.

C89 :   int, long int, unsigned long int

int, long int is 4 bytes, 32 bits on a 32-bit machine. 2147483648 exceeds the representation range of a 32-bit signed integer so 2147483648 is considered to be a unsigned long int, and then do the '-' operation. Program written in the VS 2012 compiler, compile-time-2147483648 This constant expression will have the following warning:

warning C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型

Note-The 2147483648 operation has indeed been converted to unsigned number 2147483648, and the unary negative operator is applied to the unsigned type, and the result is still unsigned. So as long as the 2147483648 constant expression is encountered, the result is 2147483648.

Now we understand-2147483648 this operation process, and then analyze the reason for this problem in the first diagram. First, let's see why (int_min==-2147483648) is true, and the numbers in the computer are in the complement notation,

INT_MIN  补码:10000000 00000000 00000000 000000002147483648 补码:10000000 00000000 00000000 00000000

The complement of the two numbers is the same, so of course they are equal. But the difference in their type leads to differences in their values. Output "1<-2147483648" then it can be understood that the expression is actually "1<2147483648" of course true. Let's see how the last Output ( -1>2147483648) is going. See vs2012 in the following warning (a>b),

warning C4018: “>”: 有符号/无符号不匹配

This warning is because a is an int type, B is a unsigned type, and there is a sign unsigned mismatch problem, when the compiler turns a into an unsigned type, and then the complement of two numbers

  a 补码:11111111 11111111 11111111 11111111  b 补码:10000000 00000000 00000000 00000000

So here a>b is true. Then the output of " -1>2147483648" can also be explained.

Int_min writes (-2147483647-1) and vs in Warning C4018: ">": An in-depth analysis of signed/unsigned mismatches.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.