Logical vs bitwise

26,345

Solution 1

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

Solution 2

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) 

Solution 3

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.

Share:
26,345

Related videos on Youtube

I159
Author by

I159

Updated on July 09, 2022

Comments

  • I159
    I159 almost 2 years

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

  • mac
    mac over 12 years
    On 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.
  • user1066101
    user1066101 over 12 years
    On 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.
  • Peter Cordes
    Peter Cordes about 2 years
    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
    Peter Cordes about 2 years
    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.)
  • Abu Shoeb
    Abu Shoeb about 2 years
    @PeterCordes thanks for pointing out a better example in your comment. I guess this can be added to the answer too.