c++ - 按位运算结果和 bool 值

标签 c++ c

令我沮丧的代码如下:

bool a = 0x00000FF0 & 0x00000FF0 == 0x00000FF0;
if (a) {
  Serial.println("True");
} else {
  Serial.println("False");
}

这会打印“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;

关于c++ - 按位运算结果和 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935397/

相关文章:

c++ - LNK2019 : Unresolved external symbol __imp__ in the . obj 文件第 1 行

c - 释放指针指向的所有内存空间?

c - 线程结束,结束整个程序?

c++ - 如果 DLL 移动到不同的位置,Windows DLL 函数的行为会有所不同

c++ - 具有类型别名的循环依赖

c++ - 为什么我的 vector<EVENTLOGRECORD> 莫名其妙地被清除了?

c++ - 使用 C 样式指针但不使用迭代器对 vector 进行向量化循环

c++ - 在 Windows 上以编程方式解压缩 AES 加密的 zip 文件

c - 如何正确传递指向结构的指针

c - 消息 "the ABI of passing struct with a flexible array member has changed in GCC 4.4"重要吗?