Shubham Kumar
Shubham Kumar

Reputation: 307

Using logical OR with cout operator

Why bitor doesn't work while using it with cout operator

This works

int a=5,b = 6,d = a bitor b;
cout << d << endl;

This is throwing error

int a=5,b=6;
cout << a bitor b << endl;

error message:

invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
  cout << a bitor b << endl;

Upvotes: 2

Views: 91

Answers (1)

songyuanyao
songyuanyao

Reputation: 172994

According to the Operator Precedence, operator<< has higher precedence than operator bitor. Then cout << a bitor b << endl; will be interpreted as

(cout << a) bitor (b << endl);

while b << endl is invalid.

You can add parentheses to specify the correct precedence, e.g.

cout << (a bitor b ) << endl;

Upvotes: 5

Related Questions