Why does if( -8 & 7) return false [duplicate]

荒凉一梦 提交于 2019-12-20 06:25:10

问题


When i try to run the following code it prints "FALSE" instead of "TRUE" Can somebody explain why the code returns false?

#include <stdio.h>

int main(void)
{
    if(-8 & 7)
    {
        printf("TRUE");
    }
    else
    {
        printf("FALSE");
    }
    return 0;
}

回答1:


-8 can be represented the following way ( I will use a byte representation

8  = 00001000
~8 = 11110111
-8 = 11111000 (~8 + 1)

That is -8 in the two-complement representation is equal tp ~8 + 1

So -8 is equal to 11111000 and 7 is equal to 00000111

11111000
&
00000111
========
00000000

that is the binary AND operation yields a false result.




回答2:


7 is represented as 00000111 in binary. -8 is represented as 11111000 in binary. The bitwise AND operation performs an AND on every bit:

00000111
&
11111000
=
00000000

Hence, the if condition is false.



来源:https://stackoverflow.com/questions/59018286/why-does-if-8-7-return-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!