使用cout输出按位运算符结果时编译报错

Compile error when using cout to output result of bitwise operator

我能做到int c= 0xF^0xF; cout << c;

但是 cout << 0xF^0xF; 不会编译。为什么?

根据C++ Operator Precedenceoperator<<的优先级高于operator^,所以cout << 0xF^0xF;等价于:

(cout << 0xF) ^ 0xF;

cout << 0xFreturnscout(即std::ostream),不能作为operator^.[=20=的操作数]

您可以添加括号来指定正确的优先级:

cout << (0xF ^ 0xF);