c - 按位运算符比较错误

标签 c macros arm bit iar

编译器: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/

相关文章:

为 ARM M4 编译和链接位置独立代码 (PIC)

将 C 转换为 MIPS

c - c 中的 pow() 函数会产生截断(或舍入)错误?

使用 pthreads 在多线程代码中计数器值意外更改

c - 为什么要用宏来调用函数

飞思卡尔iMX6q ARM处理器的gcc选项

c - 以特定格式输入(矩阵)

c++ - 如何在编译时静态比较两个字符串

ios - 在 iOS 中详细说明宏?

architecture - ARMv7中outer和inner的定义是什么?