# Bitwise Operators in Python with Examples

Python also supports a set of bitwise operators similar to other programming languages, like Java, JavaScript. Bitwise operators in Python are binary operators that work on the bits comprising ones and zeros (i.e., binary numbers) rather than decimals or hexadecimals.

Here, the term bitwise means to operate on a binary number (0 or 1). Python bitwise operator works on each bit of number. They operate in the following steps:

1. When we use bitwise operators on the operands (i.e. integer numbers), they firstly convert operands to bits, perform the operation on each bit directly.

2. Then, bitwise operators convert binary output to a decimal number, and give the output in the decimal number.

In bitwise operations, the Python interpreter internally converts the integer numbers into the binary number system that is represented by two digits, 0 or 1.

Bitwise operators may be binary operators or unary operators that work on two operands or one operand.

## Types of Bitwise Operators in Python

Python supports six bitwise operators to work on individual bits. We list them in the table below:

Operator | Name | Type |
---|---|---|

& | Bitwise AND | Binary |

| | Bitwise OR | Binary |

^ | Bitwise XOR (Exclusive OR) | Binary |

~ | Bitwise NOT | Unary |

<< | Bitwise Left Shift | Binary |

>> | Bitwise Right Shift | Binary |

Let us have a look at each bitwise operator in Python one by one with the help of example programs.

### Bitwise AND operator (&)

Bitwise AND is a binary operator that performs AND operation on each bit of two operands (i.e., numeric values). It compares the corresponding bits of both binary numbers and sets the output to 1 if both bits are 1.

If both or one of the corresponding bits is 0, it sets the output to 0. The syntax is as follows:

`A & B # Here, A and B are operands.`

We listed the truth table for this operator in the below table:

A | B | A & B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

**Example 1:**

Consider the following example code in which we will perform a bitwise AND operation between two numbers 2 and 6.

```
A = 2
B = 6
result = A & B
print("Result of (2 & 6): ", result)
```

Output: Result of (2 & 6): 2

**Explanation:**

In this example, the binary conversion of 2 is 0010 and that of 6 is 0110. Bitwise AND operator will take each bit of both binary numbers and perform AND operation as follows in the below figure.

The decimal conversion of 0010 is 2.

**Example 2:**

```
A = 25
B = 45
result = A & B
print("Result of (25 & 45): ", result)
```

Output: Result of (25 & 45): 9

**Explanation:**

In this example, the binary conversion of 25 is 011001 and that of 45 is 101101. Bitwise AND operator will take each bit of both binary numbers and perform AND operation as follows below:

25 & 45 => 011001 & 101101 => 001001 => 9.

**Example 3:**

```
A = -25
B = 45
result = A & B
print("Result of (-25 & 45): ", result)
X = True
Y = 45
result = X & Y
print("Result of (True & 10): ", result)
P = False
Q = 15
result = P & Q
print("Result of (False & 20): ", result)
```

Output: Result of (-25 & 45): 37 Result of (True & 10): 1 Result of (False & 20): 0

### Bitwise OR operator (|)

The bitwise OR operator is a binary operator that performs a boolean OR operation on each bit of its integer numbers. It is represented by a symbol | called pipe symbol.

In OR operation, each bit of the first operand (number) is compared with the corresponding bit of the second operand. If both or any of the compared bits are 1, then the OR operator sets the output to 1. Otherwise, it sets the output to 0. The syntax is as below:

`A | B`

The truth table for bitwise OR operator is as below:

A | B | A | B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 1 |

Consider the preceding truth table of bitwise OR operator. If both compared bits are 0, the output is 0. If anyone bit is 1 in both bits; the output is 1.

**Example 1:**

Let’s take an example program in which we will perform OR operation between two integer numbers 2 and 6.

```
A = 2
B = 6
result = A | B
print("Result of (2 | 6): ", result)
```

Output: Result of (2 | 6): 6

**Explanation:**

In this example, the binary conversion of 2 is 0010 and that of 6 is 0110. Bitwise OR operator will take each bit of both binary numbers and perform OR operation as follows in the below figure.

The decimal conversion of 0110 is 6.

**Example 2:**

```
A = 25
B = 45
result = A | B
print("Result of (25 | 45): ", result)
```

Output: Result of (25 | 45): 61

**Explanation:**

In this example, the binary conversion of 25 is 011001 and that of 45 is 101101. Bitwise OR operator will take each bit of both binary numbers and perform OR operation as follows below:

25 & 45 => 011001 & 101101 => 111101 => 61 (in decimal number system).

**Example 3:**

```
A = -25
B = 45
result = A | B
print("Result of (-25 | 45): ", result)
X = True
Y = 45
result = X | Y
print("Result of (True | 10): ", result)
P = False
Q = 15
result = P | Q
print("Result of (False | 20): ", result)
```

Output: Result of (-25 | 45): -17 Result of (True | 10): 45 Result of (False | 20): 15

### Bitwise XOR operator (^)

The bitwise XOR is a binary operator that performs a boolean exclusive OR (XOR) operation on each bit of its integer numbers. It is represented by a symbol ^ called cap. The general syntax to use XOR operator is as:

```
A ^ B
```

Bitwise XOR operator compares each bit of the first operand (number) with the corresponding bit of the second operand and sets the output to 1 if any one bit is 1. If both input bits are 0 or 1, then it sets the output bit to 0.

That is, if both input bits have the same value, then the output is 0. Otherwise, the output is 1 if the odd number of 1 is in input bits. Look at the below truth table to understand more clearly.

A | B | A ^ B |
---|---|---|

0 | 0 | 0 |

0 | 1 | 1 |

1 | 0 | 1 |

1 | 1 | 0 |

**Example 1:**

Let’s write a Python program in which we will perform bitwise XOR operation between two numbers 2 and 6.

```
A = 2
B = 6
result = A ^ B
print("Result of (A ^ B): ", result)
```

Output: Result of (A ^ B): 4

**Explanation:**

In the preceding example, the binary conversion of 2 is 0010 and that of 6 is 0110. Bitwise XOR operator will take each bit of both binary numbers and perform XOR operation as follows below:

2 ^ 6 => 0010 & 0110 => 0100 => 4. The decimal conversion of 0100 is 4.

**Example 2:**

```
A = 25
B = 45
result = A ^ B
print("Result of (25 ^ 45): ", result)
X = True
Y = 45
result = X ^ Y
print("Result of (True ^ 10): ", result)
P = False
Q = 15
result = P ^ Q
print("Result of (False ^ 20): ", result)
```

Output: Result of (25 ^ 45): 52 Result of (True ^ 10): 44 Result of (False ^ 20): 15

### Bitwise NOT operator (~)

The bitwise NOT is a unary operator that returns the reverse of its operand or value. It works basically as an inverter that converts all 1s to 0s, and all the 0s to 1s. Therefore, it is also called bit flip operator.

The truth table of bitwise NOT operator is as below:

A | ~A |
---|---|

0 | 1 |

1 | 0 |

**Example 1:**

Let us take an example in which we will perform bitwise NOT operation of an integer number 3.

```
A = ~3
print("Result of (~3): ", A)
```

Output: Result of (~3): -4

**Explanation:**

The bitwise NOT operator will take each bit of binary number and convert all 1s to 0s and all 0s to 1s. The NOT operator also reverses the sign bit of binary number.

Thus, the positive number becomes negative and the negative number becomes positive. Therefore, the signed binary number of 3 is 0011.

~3 => ~0011 => 1100 => -4. The decimal conversion of 0011 is -4.

**Note:**

With a signed integer, the leftmost bit is sign bit. For a +ve number, the leftmost bit is 0 and for a -ve number, the leftmost bit is 1.

For example, the signed binary representation of 3 is 0011. Here, the first 0 represents a positive number. Similarly, the signed binary representation of -4 is 1100. Here, the first 1 represents a negative number.

### Bitwise Left Shift Operator (<<)

The bitwise left shift is a binary operator that shifts the number of bits to its left position. It shifts all bits to the left according to the values specified on the right-hand side of the operator.

After performing the left shift operation on the bits, the operator fills the rightmost bits by 0. The general syntax to use bitwise left shift operator is as:

A << B

**Example 1:**

Let’s take an example program in which we will perform 1 bit and 2 bit left operation using the bitwise left shift operator on the integer number 3.

```
A = 3
B = 1
result = A << B
print("Result of (A << B): ", result)
X = 3
Y = 2
result = X << Y
print("Result of (X << Y): ", result)
```

Output: Result of (A << B): 6 Result of (X << Y): 12

**Explanation:**

a) The working of bitwise left shift operator to shift one bit of 3 to the left side is as follows:

3 << 1 => 0011 << 1=> 0110 => 6 (shifted all bits by 1 position to the left and filled the rightmost one bit by 0).

b) The working of bitwise left shift operator to shift two bits of 3 to the left side is as follows:

3 << 2 => 0011 << 2 => 1100 => 12 (shifted all bits by 2 positions to the left and filled the rightmost two bits by 0).

**Example 2:**

```
X = 25
Y = 2
result = X << Y
print("Result of (A << B): ", result)
```

Output: Result of (X << Y): 100

The working of bitwise left shift operator to shift two bits of 25 to the left side is as follows:

25 << 2 => 00011001 << 2=> 01100100 => 100 (shifted all bits by 2 positions to the left and filled the rightmost two bits by 0).

The decimal conversion of 01100100 is 100.

### Bitwise Right Shift Operator (>>)

The bitwise right shift is a binary operator that shifts the number of bits to its right position. It shifts all bits to the right according to the values specified on the right-hand side of the operator.

After performing the right shift operation on the bits, the operator fills the leftmost bits by 0. The general syntax to use bitwise right shift operator is as:

```
A >> B
```

**Example 1:**

Let’s take an example program in which we will perform 1 bit and 2 bits right operation using the bitwise right shift operator on the integer number 6.

```
A = 6
B = 1
result = A >> B
print("Result of (A >> B): ", result)
X = 6
Y = 2
result = X >> Y
print("Result of (X >> Y): ", result)
```

Output: Result of (A >> B): 3 Result of (X >> Y): 1

**Explanation:**

a) The working of bitwise right shift operator to shift one bit of 6 to the right side is as follows:

6 >> 1 => 0110 >> 1 => 0011 => 3 (shifted all bits by 1 position to the right and filled the leftmost one bit by 0).

b) The working of bitwise right shift operator to shift two bits of 6 to the right side is as follows:

6 >> 2 => 0110 >> 2 => 0001 => 1 (shifted all bits by 2 position to the right and filled the leftmost two bits by 0).

The conversion of 0011 in decimal number system is 3 and that of 0001 is 1.

**Example 2:**

Let’s take an example program where we will perform all operations using all the bitwise operators supported by the Python language. Here, we will take three numbers as inputs from the user.

```
p = int(input('Enter your first number: '))
q = int(input('Enter your second number: '))
r = int(input('Enter the number of bits to be shifted towards left and right: '))
print('Outcome of bitwise &: ', p & q)
print('Outcome of bitwise |: ', p | q)
print('Outcome of bitwise ^: ', p ^ q)
print('Outcome of bitwise ~: ', ~p)
print('Outcome of bitwise ~: ', ~q)
print('Outcome of bitwise <<: ', p << r) print('Outcome of bitwise >>: ', p >> r)
```

Output: Enter your first number: 25 Enter your second number: 35 Enter the number of bits to be shifted towards left and right: 2 Outcome of bitwise &: 1 Outcome of bitwise |: 59 Outcome of bitwise ^: 58 Outcome of bitwise ~: -26 Outcome of bitwise ~: -36 Outcome of bitwise <<: 100 Outcome of bitwise >>: 6

In this tutorial, you have learned all six types of bitwise operators supported by Python language with various examples. I hope that you will have understood the basic points of all **bitwise operators in Python** and practiced all example programs.

Thanks for reading!!!