编译器:IAR 平台:stm32f4
我有这段代码,但它没有按预期工作。 有人可以解释一下为什么吗?
#define byteGetbit(x,y) ((x) & (0x01 << (y)))
volatile t_uint8 test=0xFF;
if(byteGetbit(test,0x03)==1){ //always false
printf("hello"); //can't reach the code here
test = 0;
}
解决方法:
if(byteGetbit(test,0x03)){
printf("hello");
test = 0;
}
最佳答案
byteGetbit
会生成一个设置或清除相应位的值,因此要测试您必须检查的位设置:
if(byteGetbit(test,0x03) == 0x03)
或
if(byteGetbit(test,0x03)) // test if byteGetbit(test,0x03) != 0
关于c - 按位运算符比较错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516326/