"Non-symbolic and symbolic addition operations in C language" "deep understanding"-"Sky original"

Source: Internet
Author: User

"Unsigned and symbolic addition operations in C" "deep Understanding"-"Sky original"

First question

#include <stdio.h>
int main ()
{
unsigned int a=6;
int b=-20;
printf ("%d\n", a+b);
(a+b) >6? Puts (">6"):p UTS ("<=6");
return 0;
}

The answer is: >6 second question#include <stdio.h>
int main ()

unsigned int a=6;
int b=-2;
printf ("%d\n", a+b);
(a+b) >6 puts (">6"):p UTS ("<=6");
return 0;
} The answer is: <=6 many experienced engineers see this topic will feel that two answers are more than 6, then take it for granted, this is a lot of our people will make a mistake, the computer tells us not to trust intuition, to do the calculation.  first explain the points:1, signed and unsigned addition operations, unsigned int is higher than the level of int, so in addition operation will be automatically converted to unsigned int, note that the negative number at the time of conversion of the complement is unchanged2, negative numbers in the operation is performed in the complement of the operation, first write-2 of the binary, is the highest level of 1, that is, 0000 0000 0010This is under the 32-bit system, then the complement is the reverse plus 11111 1111 1111 1101---> 1111 1111 1111 1110for positive numbers, 6 of the complement is itself, namely 0000 0000 0000 0110 6+ (-2) can be counted, the result is 4, then why so small, not 6-2=4 so understand, many people mistakenly think will calculate a very large number is because the highest bit is 1, then it is very big, but if add up, for example, this example, the highest bit has overflowed, the number of overflow will be shed, So the answer is 4, as to why the first question is greater than the 6 principle is the same, I will not say more. Welcome to Exchange

Sina Blog: http://blog.sina.com.cn/u/2049150530
Blog Park: http://www.cnblogs.com/sky-heaven/
Know: Http://www.zhihu.com/people/zhang-bing-hua

 

"Non-symbolic and symbolic addition operations in C language" "deep understanding"-"Sky original"

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.