1 c++ bitwise-operators bitwise-and
我的问题很简单:如何int在C++中执行按位AND ?
#include <iostream>
int main() {
unsigned int foo = 3;
unsigned int bar = 6;
std::cout << foo & bar;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出3而不是输出2.
当我执行任何其他按位操作时,它也只打印第一个变量.
如何让它进行操作?
您需要在您foo & bar的周围添加括号,因为&运算符的优先级低于shift <<运算符.
std::cout << (foo & bar);
Run Code Online (Sandbox Code Playgroud)
作为旁注,我很惊讶代码编译没有括号.额外奖励:cppreference上运算符优先级规则的文档
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |