让我感到沮丧的代码如下:
bool a = 0x00000FF0 & 0x00000FF0 == 0x00000FF0; if (a) { Serial.println("True"); } else { Serial.println("False"); }
这打印“假”.我真的不明白为什么.
更多测试:
bool a = 0x00000FF0 & 0x00000FF0 == 0x00000FF0; Serial.println(a);
打印0.
和:
unsigned long a = 0x00000FF0 & 0x00000FF0; Serial.println(a,HEX);
打印FF0.
解决方法
运算符优先级,编译时带有警告:
warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
改成
bool a = (0x00000FF0 & 0x00000FF0) == 0x00000FF0;