c++logical-operatorscoutoperator-precedenceshift

Why isn't the OR (||) logical operator being properly computed?


When printing to the terminal, the OR operator is not being applied in C++.

MWE:

#include <iostream>
int main()
{
    std::cout << false || true;
    return 0;
}

Solution

  • Shift operators have higher priority than logical operators.

    So this statement

    std::cout << false || true;
    

    is equivalent to

    ( std::cout << false ) || ( true );
    

    As a result the literal false will be outputted as integer 0.

    If you want to output the literal true then you should write

    std::cout << ( false || true );