In this tutorial, you’ll learn all about Python bitwise operators. Bitwise operators in Python are used to perform various logical operations between binary numbers. Binary numbers are the numbers that are represented in the form of digits 0s and 1s. Each individual digit (0 or 1) in a binary number is called a bit. Since the bitwise operators operate on individual bits of binary numbers, they’re called “bitwise” operator.

The bitwise operators we’ll be introducing in this tutorial are:

- AND Operator
- OR Operator
- XOR Operator
- NOT Operator
- Shift Operator

## What are Binary numbers

Before we go on and study these operators, let’s walk through a brief example of binary numbers in Python.

The script below initializes a variable `bin()`

function.

```
num = 42
bin_num = bin(num)
print(bin_num)
```

**Output:**

`0b101010`

From the output, you can see that the binary value of the integer 42 is 0b101010. Here the leftmost “0b” signifies that this number is a binary number where as the integer 42 is represented via the remaining bits e.g. 101010.

Let’s manually convert the binary number 101010 back to an integer and see if it actually equals 42.

To convert a binary number to an integer, starting from the right most bit you have to multiply the corresponding bits of a binary number with the powers of 2 starting from 0.

For example to convert the integer 101010 you will have to multiply the right most bit, i.e. 0, with the 2^{0} which equals 0 x 1 = 0. Next, the second to last bit from the right is 1 so you have to multiply it with 2^{1} which equals 1 x 2 = 2. Similarly, the third last bit from the right is again 0, you have to multiply it with 2^{2} i.e. 0 x 4 = 0.

All the multiplications performed to convert the binary 101010 to integer are as follows:

0 x 2^{0}= 0 x 1 = 0 1 x 2^{1}= 1 x 2 = 2 0 x 2^{2}= 0 x 4 = 0 1 x 2^{3}= 1 x 8 = 8 0 x 2^{4}= 0 x 16 = 0 1 x 2^{5}= 1 x 32 = 32

Finally, you have to add the result of all these calculations to get the integer value. In our case, the integer value will be 0 + 2 + 0 + 8 + 0 + 32 = 42.

Now that you understand what binary numbers are, let’s study bitwise operators.

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

## Bitwise AND Operator

The bitwise AND operator, as the name suggests, performs the logical AND operation between the individual bits of two binary numbers. Let’s see an example.

The script below defines two integers 10 and 25 and converts them into binary numbers.

```
num1 = 10
print(bin(num1))
num2 = 25
print(bin(num2))
```

**Output:**

```
0b1010
0b11001
```

Let’s first manually perform the AND operations between the individual bits of the numbers 10 and 25. The result will be 1 if both the corresponding bits in the two numbers are 1. Otherwise, the result will be 0.

Don’t worry if the first number has fewer bits, e.g. 4, compared to the second number that contains 5 bits. Add a 0 to the **left** of the first integer to make the number of bits equal in both integers.

AND |

01010 |

11001 |

01000 |

If you convert the resultant binary number to integer, you will get 8 in the output as shown below:

`print(int(0b01000))`

**Output:**

`8`

Let’s now perform use Python to perform the bitwise AND operator between the numbers stored by variables

The bitwise AND operator in Python is the ampersand “&” sign.

```
result = num1 & num2
print(result)
```

**Output:**

`8`

The result shows that the bitwise AND between the binary counterparts of the integers 10 and 25 is the integer 8.

## Bitwise OR Operators

As the name suggests, the bitwise OR operator performs the OR operation between the individual bits of two binary numbers. The bitwise OR operator returns 1 if any of the corresponding bits of the numbers involved in the bitwise OR operation is 1. Let’s see an example.

The following script converts the integers 10 and 25 to binary numbers.

```
num1 = 10
print(bin(num1))
num2 = 25
print(bin(num2))
```

**Output:**

```
0b1010
0b11001
```

Let’s manually perform the OR operation between the binary numbers 01010 and 11001.

OR |

01010 |

11001 |

11011 |

If you convert the result of the OR operation performed above to an integer, you will see 27 in the output:

`print(int(0b11011))`

**Output:**

`27`

Let’s now use the bitwise OR operator in Python to perform the OR operation between the binary values of the integers 10 and 25. The pipe symbol “ | ” is used as the bitwise OR operator in Python. |

```
result = num1 | num2
print(result)
```

**Output:**

`27`

The output shows that our manual calculation is correct and the bitwise OR between the integers 10 and 25 does indeed return the integer 27.

## Bitwise XOR Operator

The Bitwise XOR operator returns 1 if the corresponding bits of the two numbers involved in the XOR operation are not the same.

Let’s see an example.

The script below converts the integers 30 and 25 into binary numbers.

```
num1 = 30
print(bin(num1))
num2 = 25
print(bin(num2))
```

**Output:**

```
0b11110
0b11001
```

Let’s manually perform the XOR operation between the two numbers.

XOR |

11110 |

11001 |

00111 |

Let’s see the integer value of the resultant binary number.

`print(0b00111)`

**Output:**

`7`

Let’s now directly perform the bitwise XOR operations in Python between binary counterparts of the integers 25 and 30 and see if we get the integer 7 in the output. The carrot “^” symbol is used for bitwise XOR operation in Python. Look at the script below:

```
result = num1 ^ num2
print(result)
```

**Output:**

`7`

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

## Bitwise NOT Operator

So far so good! Next, let’s introduce the bitwise NOT operator. The bitwise NOT operator flips the bits of a binary number. The binary value of the integer 5 is 101 as shown by the script below:

```
num1 = 5
print(bin(num1))
```

**Output:**

`0b101`

If we flip the bits of the binary value of the integer 5, we get 010. Let’s try to convert the binary 010 to integer.

`print(int(0b010))`

**Output:**

`2`

The output is 2. Let’s now apply the bitwise NOT operator on the integer 5 and see if we get 2. The bitwise NOT operator in Python is “~”.

`print(int(~num1))`

**Output:**

`-6`

Instead of the integer 2 that we manually calculated, we get -6 when we apply the NOT operator on the integer 5.

Why is this the case?

To answer this question, you have to understand that binary numbers are not unsigned and can be both negative and positive. The negative binary numbers start with a minus sign.

When you apply a NOT operator to a positive number, the positive integer is converted to a negative integer and a minus sign is prefixed to it.

Okay, that makes sense so let’s try to print the binary value of the integer -6 and see if we get a minus sign.

`print(bin(-6))`

**Output:**

`-0b110`

The output shows that indeed the binary value for the integer -6 contains a negative sign.

But a question remains. How did we get the binary value of 110 when we applied the NOT operator to the binary value of 5 e.g. 101?

The answer is that the NOT operator actually performs two tasks. It flips the sign of the binary number i.e converts positive to negative and negative to positive *and* it takes what’s called **two’s complements** of the binary bits.

What do we mean by that? Let’s try to find two’s complement of the binary number 101 and prefix a negative sign to it and see if we get -110 i.e. -6.

To take one’s complement you have to reverse the already flipped bits of a number. If you flip the binary value of the integer 101, you get 0b010. If you take one’s complement of the binary value 0b010, you get 0b101.

```
flip_5 = 0b010
ones_complement = 0b101
```

To take the two’s complement, you just have to add the one’s complement with the binary value for the integer 1 (0b1 or 0b001). Look at the following script. The result is -6, which we got earlier when we applied the NOT operator to the integer 5.

```
twos_complement = 0b101 + 0b001
not_5 = int(-twos_complement)
print(not_5)
```

**Output:**

`-6`

An easier way of applying the bitwise NOT operator to a number is by simply reversing the sign of the number and adding 1 to the original number.

For example, the following script finds the bitwise NOT for the integer 5.

`-(0b101 + 0b001)`

**Output:**

`-6`

## Bitwise Shift operators

The bitwise shift operator shifts the bits in a binary number N places to the left or right.

### Bitwise Right Shift Operator

To shift the bits in a binary number to the right, the double greater than “»” symbol is used. The bits on the right most side of the binary number are truncated where as the bit places that become vacant on the left most side are filled with 0s.

Let’s see an example of how we can manually shift bits in a binary number to the right. The following script prints the binary value for the integer 30.

```
num1 = 30
print(bin(num1))
```

**Output:**

`0b11110`

To shift the above binary number 2 places to the right, we truncate the two right most bits i.e. 10. Next, the two places that become vacant on the left are filled with 0. The binary number we get by shifting 0b11110 2 places to the right is 0b00111.

Let’s convert this binary number to integer.

`print(int(0b00111))`

**Output:**

`7`

We get 7 in the output.

Let’s now directly apply the bitwise right shift operator “»” to shift the integer 30 two places to the right and see if we actually get 7.

```
shift_right = num1 >> 2
print(int(shift_right))
```

**Output:**

`7`

### Bitwise Left Shift Operator

The bitwise left shift operator shifts the bits in a binary number to the left. The shifts that become vacant on the right hand side are replaced by 0. The double less than “«”” symbol is used as the bitwise left shift operator.

Let’s see an example. The following script prints the binary value for the integer 7.

```
num1 = 7
print(bin(num1))
```

**Output:**

`0b111`

To move the above binary number 2 places to the left, you simply have to append two 0s to the right.

`print(int(0b11100))`

**Output:**

`28`

In the output we get 28.

Let’s now directly try to implement the bitwise left shift operator on the binary value of the integer 7 and see if we get the integer 28 in the output.

```
shift_left = num1 << 2
print(int(shift_left))
```

**Output:**

`28`

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.