yash
yash

Reputation: 87

Invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char> >' and 'unsigned char')

want to apply OR operation between two numbers, but showing the above error , hence am unable to understand what might be the problem!

#include <iostream>
using namespace std;
int main(){
unsigned char a,b;
cin>>a>>b;       //input two number
cout << a | b;   //applying bitwise OR operation
}

Upvotes: 0

Views: 798

Answers (2)

Nathan Pierson
Nathan Pierson

Reputation: 5565

<< has higher operator precedence than |, so by default it interprets cout << a | b; as though you'd written (cout << a) | b;. To get what you want, rewrite your expression as cout << (a | b);.

Upvotes: 1

Ayan Bhunia
Ayan Bhunia

Reputation: 549

You just need to write the expression in parentheses . cout << (a | b);

Upvotes: 0

Related Questions