tags:

views:

151

answers:

4

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?

+4  A: 

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.

Mark Byers
KennyTM
A: 

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:

And & 1 if both bits are 1, 0 otherwise
Or  | 1 if either bit is 1
Xor ^ 1 if the bits are different, 0 if they're the same
Not ~ Flip each bit

Some of the uses of bitwise operations:

1) Setting and Clearing Bits

Boolean operations:

>>> k = True
>>> z = False
>>> k & z  # and
False
>>> k | z  # or
True
>>> 
pyfunc
Matthew Rankin
Yes .. Yes .. Thanks , I do mean that.
pyfunc
+1  A: 

The hint is in the name:

  • Boolean operators are for performing logical operations (truth testing common in programming and formal logic)
  • Bitwise operators are for "bit-twiddling" (low level manipulation of bits in byte and numeric data types)

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.

Tendayi Mawushe
+3  A: 

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:

  1. and and or 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.
  2. (Only applies to some? languages with operator overloading, including Python:) & and | are regular operators and can be overloaded - and and or are forged into the language (although at least in Python, the special method for coercion to boolean may have side effects).
  3. (only applies to a few languages [see KennyTM's comment]:) and and or return (always? never really understand this, nor did I need it) 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 since a few years). 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.

delnan
Point #3 applies to Ruby, Perl and Javascript as well. It isn't that Python-specific.
KennyTM