Boolean Manipulation

this code

#include <iostream> using namespace std; int main(){ bool t=false; cout<<t &&(!t)<<endl; return 0; } 

shows me such an error

invalid operands of type "bool" and "" for binary 'Operator <<'

What's wrong? I can’t understand this, explain to me. I think && and ! defined in C ++.

So what's wrong?

+4
source share
5 answers

"invalid operands of types bool 'and' 'to binary' <<'"

This means that the second operator << trying to execute on (! T) and 'endl'.

<< takes precedence over && , so your cout statement is executed as follows:

(cout << t ) && ( (!t) << endl );

Add brackets to fix this:

cout << (t && (!t) ) << endl ;

Look here for the order of operations when statements are not evaluated as expected.

+16
source

Add parentheses to get operator precedence:

 cout << (t && !t) << endl; 

Equivalent:

 cout << false << endl; 
+6
source

&& has a lower priority than << , so the operator evaluates to (cout << t) && (!t << endl);

priority C ++ operators

+3
source

You need more brackets:

 cout << (t && !t) << endl; 
+2
source

The problem is with operator precedence, since && has a lower priority than << .

 cout<<(t && (!t))<<endl; // ok! 

Also, for any bool t variable, the expression t && (!t) always leads to false , and t || (!t) t || (!t) always leads to true . :)

+2
source

All Articles