Search code examples
c++compiler-errorsoperator-precedence

Compile error when using cout to output result of bitwise operator


I can do this int c= 0xF^0xF; cout << c;

But cout << 0xF^0xF; won't compile. Why?


Solution

  • According to C++ Operator Precedence, operator<< has higher precedence than operator^, so cout << 0xF^0xF; is equivalent with:

    (cout << 0xF) ^ 0xF;
    

    cout << 0xF returns cout (i.e. a std::ostream), which can't be used as the operand of operator^.

    You could add parentheses to specify the correct precedence:

    cout << (0xF ^ 0xF);