【发布时间】:2013-10-22 11:14:45
【问题描述】:
编译器: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;
}
【问题讨论】:
-
printf("%d\n", byteGetbit(test, 0x03)); -
@user1111998 你的逻辑
if(byteGetbit(test,0x03)==1)和#define byteGetbit(x,y) ((x) & (0x01 << (y)))总是产生不等于 1 的结果。这主要是因为左移三次显然会导致乘法,因此你的结果应该总是更大x 的所有非零正值都大于 1。