&和!=的优先级

写了个小bug,基础知识再复习。
这样的一段程序:

#include <stdio.h>
#include <stdint.h>

int main()
{
    
    
	_Bool flag1, flag2;
	flag1 = (0x54 & 0x10 != 0);
	flag2 = ((0x54 & 0x10) != 0);

	return 0;
}

结果是flag1为false,flag2为true。
要注意的就是!=的优先级是高于&的。对于flag1,先判断0x10 != 0为ture,也就是1,再进行0x54 & 1,0x54用二进制表示是0101 0100,因此位与的结果也就是0,即false。对于flag2,则先进行0x54 & 0x10,得到0x10,0x10显然不等于0,故而flag2为true。

猜你喜欢

转载自blog.csdn.net/u013213111/article/details/103059743