[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