[Symbol-free and symbolic addition operations in C language] [deep understanding] -- [sky Original], addition sky

Source: Internet
Author: User

[Symbol-free and symbolic addition operations in C language] [deep understanding] -- [sky Original], addition sky
Question 1

# Include <stdio. h>
Int main ()
{
Unsigned int a = 6;
Int B =-20;
Printf ("% d \ n", a + B );
(A + B)> 6? Puts ("> 6"): puts ("<= 6 ");
Return 0;
}

Answer:> 6. Question # include <stdio. h>
Int main ()
{
Unsigned int a = 6;
Int B =-2;
Printf ("% d \ n", a + B );
(A + B)> 6? Puts ("> 6"): puts ("<= 6 ");
Return 0;
} The answer is: <= 6 many experienced engineers will think that the two answers are greater than 6 after seeing this question. That's a question that many of us may make mistakes, the computer tells us not to trust intuition, but to perform computation. First, the following points are described: 1. the unsigned int level is higher than that of the int level for the signed and unsigned addition operations. Therefore, the unsigned int level is automatically converted to the unsigned int level during addition operations, note that the complement of the negative number is not changed during conversion. 2. The negative number is calculated by the complement code. First, the binary value of-2 is written, that is, the highest bit is 1, that is, 1000 0000 0000 0010 this is in a 32-bit system, so the complement code is to apply the 11111 1111 1111 1101 ---> 1111 1111 1111 1110. For a positive number, the 6 complement code is itself, that is, 0000, 0000, 0000, 0110, 6 + (-2). The result is 4. Why is it so small? It's not 6-2 = 4, many people mistakenly think that a large number will be calculated because the maximum bit is 1, so it will be very large, but if it is added up, for example, in this example, the maximum bit has exceeded, the number of overflow will be removed, so the answer is 4. Why is the principle of the first question greater than 6 the same? I I will not go into detail. Welcome

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

 

Related Article

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.