- Now that you have learnt about bitwise operators, you must have realized
&
is very different from&&
. Similarily|
is very different from||
-
&
or|
work on integers performing the operator on each corresponding bits. However,&&
or||
work on boolean values ( Any non zero value is true ) to produce a boolean result.
Example :
2 | 1 = 3
2 || 1 = true
2 & 1 = 0
2 && 1 = true
- Very similar to the fact that
you can rewriteA = A + B
asA += B
,
you can rewrite
A = A | B
asA |= B
or
A = A & B
asA &= B