# Python Bitwise Operators with Syntax and Example

Free Python course with 25 real-time projects Start Now!!

In this Python Bitwise Operators Tutorial, we will discuss Python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operators in Python Programming.

Along with this, we will discuss syntax and examples of Python Bitwise Operators.

So, let’s start the Python Bitwise Operators Tutorial.

Python Bitwise Operators with Syntax and Example

## What is Python Bitwise Operators?

Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole.

To take an example, let’s see the ‘and’ and ‘&’ operators for the same thing.

Let’s take two numbers- 5 and 7. We’ll show you their binary equivalents using the function bin().

>>> bin(5)

**Output**

>>> bin(7)

**Output**

Now let’s try applying ‘and’ and ‘&’ to 5 and 7.

>>> 5 and 7

**Output**

>>> 5&7

**Output**

You would have expected them to return the same thing, but they’re not the same. One acts on the whole value, and one acts on each bit at once.

Actually, ‘and’ sees the value on the left. If it has a True Boolean value, it returns whatever value is on the right.

Otherwise, it returns False. So, here, 5 and 7 is the same as True and 7. Hence, it returns 7.

However, 5&7 is the same as 101&111. This results in 101, which is binary for 5. Let’s look at each of these operators bit by bit (pun intended).

Let’s move ahead with next Python Bitwise Operator

### 1. Python Bitwise AND (&) Operator

1 has a Boolean value of True, and 0 has that of False. Take a look at the following code.

>>> True/2

**Output**

>>> False*2

**Output**

This proves something. Now, the binary and (&) takes two values and performs an AND-ing on each pair of bits.

Let’s take an example.

>>> 4 & 8

Binary for 4 is 0100, and that for 8 is 1000. So when we AND the corresponding bits, it gives us 0000, which is binary for 0. Hence, the output.

The following are the values when &-ing 0 and 1.

**Python Bitwise Operators – AND Operators**

0 & 0 | 0 |

0 & 1 | 0 |

1 & 0 | 0 |

1 & 1 | 1 |

As you can see, an &-ing returns 1 only if both bits are 1.

You cannot, however, & strings.

>>> '$'&'%'

**Output**

Traceback (most recent call last):File “<pyshell#30>”, line 1, in <module>’$’&’%’

TypeError: unsupported operand type(s) for &: ‘str’ and ‘str’

Since Boolean values True and False have equivalent integer values of 1 and 0, we can & them.

>>> False&True

**Output**

>>> True&True

**Output**

Let’s try a few more combinations.

>>> 1&True

**Output**

>>> 1.0&1.0

**Output**

Traceback (most recent call last):File “<pyshell#36>”, line 1, in <module>1.0&1.0

TypeError: unsupported operand type(s) for &: ‘float’ and ‘float’

You can also type your numbers directly in binary, as we discussed in section 6a in our Python Numbers tutorial.

>>> 0b110 & 0b101

**Output**

Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.

### 2. Python Bitwise OR (|) Operators

Now let’s discuss Python Bitwise OR (|) Operator

Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.

**Python Bitwise Operators – OR Operators**

0|0 | 0 |

0|1 | 1 |

1|0 | 1 |

1|1 | 1 |

>>> 6|1

**Output**

This is the same as the following.

>>> 0b110|0b001

**Output**

Let’s see some more examples.

>>> True|False

**Output**

Let’s move to another Python Bitwise Operator

### 3. Python Bitwise XOR (^) Operator

XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.

**Python Bitwise Operators – XOR Operators**

0^0 | 0 |

0^1 | 1 |

1^0 | 1 |

1^1 | 0 |

Let’s take a few examples.

>>> 6^6

Here, this is the same as 0b110^0b110. This results in 0b000, which is binary for 0.

>>> 6^0

**Output**

This is equivalent to 0b110^0b000, which gives us 0b110. This is binary for 6.

>>> 6^3

**Output**

Here, 0b110^0b011 gives us 0b101, which is binary for 5.

Now let’s discuss Bitwise 1’s Complement (~)

### 4. Python Bitwise 1’s Complement (~)

This one is a bit different from what we’ve studied so far. This operator takes a number’s binary, and returns its one’s complement.

For this, it flips the bits until it reaches the first 0 from right. ~x is the same as -x-1.

>>> ~2

**Output**

>>> bin(2)

**Output**

>>> bin(-3)

**Output**

To make it clear, we mention the binary values of both. Another example follows.

>>> ~45

**Output**

>>> bin(45)

**Output**

>>> bin(-46)

**Output**

### 5. Python Bitwise Left-Shift Operator (<<)

Finally, we arrive at left-shift and right-shift operators. The left-shift operator shifts the bits of the number by the specified number of places.

This means it adds 0s to the empty least-significant places now. Let’s begin with an unusual example.

>>> True<<2

**Output**

Here, True has an equivalent integer value of 1. If we shift it by two places to the left, we get 100. This is binary for 4.

Now let’s do it on integers.

>>> 2<<1

**Output**

10 shifted by one place to the left gives us 100, which is, again, 4.

>>> 3<<2

**Output**

Now, 11 shifted to the left by two places gives us 1100, which is binary for 12.

Now let’s move to Next Python Bitwise Operator

### 6. Python Bitwise Right-Shift Operator (>>)

Now we’ll see the same thing for right-shift. It shifts the bits to the right by the specified number of places.

This means that those many bits are lost now.

>>> 3>>1

**Output**

3 has a binary value of 11, which shifted one place to the right returns 1. But before closing on this tutorial, we’ll take one last example.

Let’s check what’s the decimal value for 11111.

>>> int(0b11111)

**Output**

Now, let’s shift it three places to the right.

>>> 31>>3

**Output**

As you can see, it gives us 3, whose binary is 11. Makes sense, doesn’t it?

This was all about the Python Bitwise Operators.

### Python Interview Questions on Bitwise Operators

- What is the purpose of Python Bitwise Operator?
- Explain Python Bitwise Operator with an example?
- Are Python Bitwise Operators faster?
- What is Bitwise Not Operator?
- Where are Python Bitwise Operators used?

## Conclusion

While they’re not so common in real-world programming, Python Bitwise Operators do find their use in places like encryption, compression, and byte manipulation.

Thanks, very good. My issue using bitwise in Python is understanding what types can or should be used – is an int assumed? Is that 4 bytes? I get errors when trying to use byte types in python

i don’t understand how 7 comes when in and 5 and 7

sorry i didn’t read below info…

Thank you so much for such great resource.

I used to work on embedded project where we used python a lot to manipulate the bits.

I found, that using bitwise operations, sometimes you code could be mode compact even though for me it takes some time to understand. Let say i can convert the bits to string then change the way i want then convert back (this simple example) which is not optimal.

I am wondering is there any set of standardized shortcuts (bitwise tricks, bitwise coockbook) that developers are using, that i can learn and used in future or it comes with experience?

I am sorry cannot give you exact example of code.

Thanks in advance

sorry was not able to edit my comment..

I found this code from stackoverflow.

how user named(travc) comes up with this idea (twos complement)

please see the code below

def twos_comp(val, bits):

“””compute the 2’s complement of int value val”””

if (val & (1 << (bits – 1))) != 0: # if sign bit is set e.g., 8bit: 128-255

val = val – (1 << bits) # compute negative value

return val # return positive value as is

How to find bitwise AND of two binary numbers without using the logical symbols ‘&’. Please help me with this problem