# 6 Bitwise Operators in Python | Coding Example

There are different ways of representing numeric data such as binary, octal, decimal, and hexadecimal.

In the computer, every number is saved as binary bits (internally).

As part of this tutorial, you will learn how to covert integer into binary number and how you can perform binary operations.

In the end, I also explain the use cases of binary operations in programming. You will find why performing bitwise operations are very useful.

Let’s begin.

Table of Contents

#### Convert Integer into Binary String in Python

Integer is one of the basic numeric data types in Python.

You can use the `bin()`

method to convert integer value into a binary string.

bin_num = bin(21) print(bin_num)

**Output:**

0b10101

In the output, the first two characters ‘0b’ are there to represent the binary data in the string. Remaining character string ‘10101’ is the actual value when you convert an integer value to binary.

You can use the below calculator to verify the integer to binary conversion.

The various operations can be performed on the binary bits.

By default, every integer in Python uses decimal representation. But you can perform binary operations on integers.

To perform binary operations on integer, you don’t need to convert an integer number to binary. Python handles it gracefully.

Here are the binary operations you can perform on integer number in Python.

#### Bitwise operators in Python (Tabular form)

Assume ‘a’ and ‘b’ are two integers. Below is the syntax used for performing various bit operations.

Operator | Operation | Syntax | ||
---|---|---|---|---|

1 | & | Bitwise AND | a & b | |

2 | | | Bitwise OR | a | b | |

3 | ~ | Bitwise NOT | ~a | |

4 | ^ | Bitwise XOR | a ^ b | |

5 | >> | Bitwise right shift | x>> | |

6 | << | Bitwise left shift | x<< |

Now let’s see the example of these each bit-wise operators.

#### Example 1: Bitwise AND in Python

a = 4 b = 5 print("a = ", a) print("b = ", b) print("a & b = ", a & b)

**Output:**

a = 4 b = 5 a & b = 4

#### Example 2: Bitwise OR in Python

a = 4 b = 5 print("a = ", a) print("b = ", b) print("a | b = ", a | b)

**Output:**

a = 4 b = 5 a | b = 5

#### Example 3: Bitwise NOT in Python

a = 4 print("a = ", a) print("~a = ", ~a)

**Output:**

a = 4 ~a = -5

#### Example 4: Bitwise XOR in Python

a = 4 b = 5 print("a = ", a) print("b = ", b) print("a ^ b = ", a ^ b)

**Output:**

a = 4 b = 5 a ^ b = 1

#### Example 5: Bitwise Right Shift in Python

a = 4 print("a = ", a) print("a >> 1 = ", a >> 1)

**Output:**

a = 4 a >> 1 = 2

#### Example 6: Bitwise Left Shift in Python

a = 4 print("a = ", a) print("a << 1 = ", a << 1)

**Output:**

a = 4 a << 1 = 8

These are the simple coding examples for performing bitwise operations.

#### Why Use of the Bit wise Operators in Python

These operations are very useful when you want to manipulate the binary bits in the number.

If you check the competitive coding challenge questions, many times the logic evolves around bit operations.

**For example:**

- Binary XOR operation can be used to find the unique number in the List in O(n) time.
- Binary XOR operation can also be used to swap to numbers.

**Conclusion:**

In this tutorial, we have learned different bit operations that can be performed on the number in Python.

We have also listed down all examples for AND, OR, NOT, XOR, LEFT SHIFT and RIGHT SHIFT binary operations.

Try to implement these binary operations while solving coding questions.

If you have any doubts or want to discuss anything related to bitwise operators in Python, comment below.