What the different between logical operators and
, or
and bitwise analogs &
, |
in usage? Is there any difference in efficiency in various solutions?

- 29,741
- 31
- 97
- 132
3 Answers
Logical operators operate on logical values, while bitwise operators operate on integer bits. Stop thinking about performance, and use them for they're meant for.
if x and y: # logical operation
...
z = z & 0xFF # bitwise operation

- 125,936
- 27
- 200
- 224
-
4On the performance issue: normally the difference between these low-level operations impacts on the overall performance of the program in the order of 10**6 or more iterations. If you (OP!) need to perform that operation that many times, chances are you should switch altogether to a numeric/scientific library like numpy/scipy. – mac Dec 07 '11 at 16:06
-
19On the performance issue. Since `and` and `or` are "short-circuit" operators that work left-to-right, you'll find that `a and (something hellishly expensive)` runs **completely** differently from `a & (something hellishly expensive)` when `a` is False. Short-circuit evaluation isn't a negligible effect at all. It's profound. – S.Lott Dec 07 '11 at 16:33
Bitwise = Bit by bit checking
# Example
Bitwise AND: 1011 & 0101 = 0001
Bitwise OR: 1011 | 0101 = 1111
Logical = Logical checking or in other words, you can say True/False
checking
# Example
# both are non-zero so the result is True
Logical AND: 1011 && 0101 = 1 (True)
# one number is zero so the result is False
Logical AND: 1011 && 0000 = 0 (False)
# one number is non-zero so the result is non-zero which is True
Logical OR: 1011 || 0000 = 1 (True)
# both numbers are zero so the result is zero which is False
Logical OR: 0000 || 0000 = 0 (False)

- 4,747
- 2
- 40
- 45
-
A good example would be `2 & 1` producing (integer) `0`, because their set bits don't intersect. But `2 && 1` is (boolean) `true` because `(2 != 0) && (1 != 0)` is `true && true`. That's important for understanding that `&` isn't a drop-in replacement except for not short-circuiting. – Peter Cordes Mar 17 '22 at 17:24
-
I don't think `1011 && 0000` is a very good illustration because it's 0 or false with & or &&. (In like C++, even the type is different: `&&` produces a `bool`, `&` produces an integer of the type of the inputs. If Python has a bool type, I'd assume it's like that.) – Peter Cordes Mar 17 '22 at 17:25
-
@PeterCordes thanks for pointing out a better example in your comment. I guess this can be added to the answer too. – Abu Shoeb Mar 17 '22 at 17:57
Logical operators are used for booleans, since true
equals 1 and false
equals 0. If you use (binary) numbers other than 1 and 0, then any number that's not zero becomes a one.
Ex: int x = 5;
(101 in binary) int y = 0;
(0 in binary) In this case, printing x && y
would print 0
, because 101 was changed to 1, and 0 was kept at zero: this is the same as printing true && false
, which returns false
(0).
On the other hand, bitwise operators perform an operation on every single bit of the two operands (hence the term "bitwise").
Ex: int x = 5; int y = 8;
printing x | y
(bitwise OR) would calculate this:
000101
(5)| 1000
(8)
-----------
= 1101
(13)
Meaning it would print 13
.

- 13
- 1
- 3

- 426
- 3
- 8
- 20