I'm just wondering why we usually use logical OR ||
between two booleans not bitwise OR |
, though they are both working well.
I mean, look at the following:
if(true | true) // pass
if(true | false) // pass
if(false | true) // pass
if(false | false) // no pass
if(true || true) // pass
if(true || false) // pass
if(false || true) // pass
if(false || false) // no pass
Can we use |
instead of ||
? Same thing with &
and &&
.
1).(expression1 | expression2), | operator will evaluate expression2 irrespective of whether the result of expression1 is true or false.
Example:
2).(expression1 || expression2), || operator will not evaluate expression2 if expression1 is true.
Example:
| = bitwise or, || = logic or
Logical
||
and&&
check the right hand side only if necessary. The|
and&
check both all the time.For example:
Rewrite it:
Another example:
Rewrite it: