Difference Between Bitwise and Logical Operators First, logical operators work on boolean expressions and return boolean values (either true or false), whereas bitwise operators work on binary digits of integer values (long, int, short, char, and byte) and return an integer.
a) The logical and operator '&&' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&' work on Integral (short, int, unsigned, char, bool, unsigned char, long) values and return Integral value.
The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data.
Using bitwise operations for bool helps save unnecessary branch prediction logic by the processor, resulting from a 'cmp' instruction brought in by logical operations. Replacing the logical with bitwise operations (where all operands are bool) generates more efficient code offering the same result.
Here are a couple of guidelines:
The short-circuiting behaviour is useful in expressions like this:
if x is not None and x.foo == 42:
# ...
This would not work correctly with the bitwise &
operator because both sides would always be evaluated, giving AttributeError: 'NoneType' object has no attribute 'foo'
. When you use the boolean and
operator the second expression is not evaluated when the first is False. Similarly or
does not evaluate the second argument if the first is True.
In theory, and
and or
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
and or
short-circuiting, e.g. True or sys.exit(1)
will not exit, because for a certain value of the first operand (True or ...
, False and ...
), the second one wouldn't change the result so 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, while and
and or
are forged into the language (although the special method for coercion to boolean may have side effects).
and
and or
return the value of an operand instead of True
or False
. This doesn't change the meaning of boolean expressions in conditions - 1 or True
is 1
, but 1
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). For &
and |
, the result type depends on how the operands overload the respective special methods (True & False
is False
, 99 & 7
is 3
, for sets it's unions/intersection...).
But even when e.g. a_boolean & another_boolean
would work identically, the right solution is using and
- simply because and
and or
are associated with boolean expression and condition while &
and |
stand for bit twiddling.
Here's a further difference, which had me puzzled for a while just now: because &
(and other bitwise operators) have a higher precedence than and
(and other boolean operators) the following expressions evaluate to different values:
0 < 1 & 0 < 2
versus
0 < 1 and 0 < 2
To wit, the first yields False
as it is equivalent to 0 < (1 & 0) < 2
, hence 0 < 0 < 2
, hence 0 < 0 and 0 < 2
.
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, but and
and or
will return value error.
To be on the safe side, you can use the numpy logic functions.
np.array([True, False, True]) | np.array([True, False, False])
# array([ True, False, True], dtype=bool)
np.array([True, False, True]) or np.array([True, False, False])
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
np.logical_or(np.array([True, False, True]), np.array([True, False, False]))
# array([ True, False, True], dtype=bool)
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.
The general rule is to use the appropriate operator for the existing operands. Use boolean (logical) operators with boolean operands, and bitwise operators with (wider) integral operands (note: False is equivalent to 0, and True to 1). The only "tricky" scenario is applying boolean operators to non boolean operands.
Let's take a simple example, as described in [SO]: Python - Differences between 'and' and '&': 5 & 7
vs. 5 and 7
.
For the bitwise and (&), things are pretty straightforward:
5 = 0b101 7 = 0b111 ----------------- 5 & 7 = 0b101 = 5
For the logical and, here's what [Python.Docs]: Boolean operations states (emphasis is mine):
(Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument.
Example:
>>> 5 and 7 7 >>> 7 and 5 5
Of course, the same applies for | vs. or.
Boolean operation are logical operations.
Bitwise operations are operations on binary bits.
Bitwise operations:
>>> k = 1
>>> z = 3
>>> k & z
1
>>> k | z
3
The operations:
&
: 1 if both bits are 1, otherwise 0|
: 1 if either bit is 1, otherwise 0^
: 1 if the bits are different, 0 if they're the same~
': Flip each bitSome of the uses of bitwise operations:
Boolean operations:
>>> k = True
>>> z = False
>>> k & z # and
False
>>> k | z # or
True
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With