Boolean operators vs Bitwise operators

2018-12-31 19:30发布

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?

8条回答
公子世无双
2楼-- · 2018-12-31 19:56

Here are a couple of guidelines:

  • Boolean operators are usually used on boolean values but bitwise operators are usually used on integer values.
  • Boolean operators are short-circuiting but bitwise operators are not short-circuiting.

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 andoperator the second expression is not evaluated when the first is False. Similarly or does not evaluate the second argument if the first is True.

查看更多
何处买醉
3楼-- · 2018-12-31 19:57

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 '&' [duplicate]: 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 3]: Boolean operations states:

(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.

查看更多
登录 后发表回答