I am confused as to when I should use a Boolean vs Bitwise operators
and vs &, or vs |
Could someone enlighten me as to when do i use each and when will using one over the other affect my results?
I am confused as to when I should use a Boolean vs Bitwise operators
and vs &, or vs |
Could someone enlighten me as to when do i use each and when will using one over the other affect my results?
Here's a further difference, which had me puzzled for a while just now: because
&
(and other bitwise operators) have a higher precedence thanand
(and other boolean operators) the following expressions evaluate to different values:versus
To wit, the first yields
False
as it is equivalent to0 < (1 & 0) < 2
, hence0 < 0 < 2
, hence0 < 0 and 0 < 2
.Boolean 'and' vs. Bitwise '&':
Pseudo-code/Python helped me understand the difference between these:
In theory,
and
andor
come straight from boolean logic (and therefore operate on two booleans to produce a boolean), while&
and|
apply the boolean and/or to the individual bits of integers. There are a lot lot of questions here on how the latter work exactly.Here are practical differences that potentially affect your results:
and
andor
short-circuiting, i.e.True or sys.exit(1)
will not exit, because for a certain value (True or ...
,False and ...
) of the first operand, the second one wouldn't change the result = does not need to be evaluated. But|
and&
don't short-circuit -True | sys.exit(1)
throws you outta the REPL.&
and|
are regular operators and can be overloaded -and
andor
are forged into the language (although at least in Python, the special method for coercion to boolean may have side effects).and
andor
return (always? never really understand this, nor did I need it) the value of an operand instead ofTrue
orFalse
. This doesn't change the meaning of boolean expressions in conditions -1 or True
is1
, but1
is true, too. But it was once used to emulate a conditional operator (cond ? true_val : false_val
in C syntax,true_val if cond else false_val
in Python since a few years). For&
and|
, the result type depends on how the operands overload the respective special methods (True & False
isFalse
,99 & 7
is3
, for sets it's unions/intersection...).But even when e.g.
a_boolean & another_boolean
would work identically, the right solution is usingand
- simply becauseand
andor
are associated with boolean expression and condition while&
and|
stand for bit twiddling.Boolean operation are logical operations.
Bitwise operations are operations on binary bits.
Bitwise operations:
The operations:
Some of the uses of bitwise operations:
1) Setting and Clearing Bits
Boolean operations:
If you are trying to do element-wise boolean operations in
numpy
, the answer is somewhat different. You can use&
and|
for element-wise boolean operations, butand
andor
will return value error.To be on the safe side, you can use the numpy logic functions.
The hint is in the name:
While it is possible and indeed sometimes desirable (typically for efficiency reasons) to perform logical operations with bitwise operators, you should generally avoid them for such purposes to prevent subtle bugs and unwanted side effects.
If you need to manipulate bits, then the bitwise operators are purpose built. The fun book: Hackers Delight contains some cool and genuinely useful examples of what can be achieved with bit-twiddling.