使用COUT来输出位运算符的结果时编译错误

Compile error when using cout to output result of bitwise operator

本文关键字:结果 编译 错误 运算符 COUT 输出 出位 使用      更新时间:2023-10-16

我可以做这个 int c= 0xF^0xF; cout << c;

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

根据C 操作员优先级,operator<<的优先级高于operator^,因此cout << 0xF^0xF;等于:

(cout << 0xF) ^ 0xF;

cout << 0xF返回cout(即std::ostream),它不能用作operator^的操作数。

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

cout << (0xF ^ 0xF);