 # Everything You Need To Know About Bitwise Operators In Python Python is one of the most popular programming languages in the world of today. A lot can be achieved on Python, thanks to its great versatility and the number of features it brings to the table. In this article we will explore Bitwise Operators In Python and following pointers as well,

So let us get started then,

Bitwise operators are one of the key aspects of programming in Python and in this article we will discuss about the various types of bitwise operators, their uses and how can include them in your day to day coding. Let’s begin!

## What are Bitwise Operators in Python?

Bitwise operators in Python are functions and or methods that are used to perform bit operations. In simple terms it is the process of converting integers and strings into bits that are 0 and 1. By using these operators, you are urging Python to either shift them from left to right or convert them into a sequence of 0s and 1s. For example,  0100, 1100, 1000, 1001.

In order to understand this better, take a look at the example below.

x = 6 and y = 8

When converted, their values in binary form will be x = 0110 and y = 1000.

## Bitwise Operators in Python

Mentioned below are some of the most significant bitwise operators in Python and their uses.

1. &: Known as Bitwise AND. Example, X & Y = 0000.
2. ^: Known as Bitwise exclusive OR. Example, X ^ Y = 1110.
3. |: Known as Bitwise OR. Example, X | Y = 1110.
4. ~: Known as Bitwise complement. Example, ~X = 00001001.
5. <<: Known as Shift Left. Example, X << 1 = 00001100. Here the bits will be moved 1 step to the left.
6. >>:  Known as Shift Right. Example, Y >> 1 = 00000100.

A bitwise operator in Python performs its tasks as set forth in the Truth table. To understand this better, take a look at the truth table for different operators below.

X Y X & Y X | Y X ^ Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

## Example of Bitwise Operators

Now that you have understood the basic concept behind the function of bitwise operators, let us take an example to clarify the concept further. In the example shared below, we are taking into account two variables a and b, and inserting values 9 and 65 into them.

```a = 9
b = 65
print("Bitwise AND Operator On 9 and 65 is = ", a & b)
print("Bitwise OR Operator On 9 and 65 is = ", a | b)
print("Bitwise EXCLUSIVE OR Operator On 9 and 65 is = ", a ^ b)
print("Bitwise NOT Operator On 9 is = ", ~a)
print("Bitwise LEFT SHIFT Operator On 9 is = ", a << 1) print("Bitwise RIGHT SHIFT Operator On 65 is = ", b >> 1)```

In the above example, we declared two variables a and b and shared the values 9 and 65 with them. When converted into binary,  9 = 00001001 and 65 = 01000001.

Calculations

For the above program, let us manually calculate what the results might be.

1. Bitwise AND Operation = a & b. Analysis: 00001001 & 01000001 = 00000001 = 1
2. Bitwise OR Operation = a | b. Analysis: 00001001 | 01000001 = 01001001 = 73
3. Bitwise Exclusive OR Operation in Python = a ^ b. Analysis: 00001001 ^ 01000001 = 01001000 = 72
4. Right Shift Operation in Python = b >> 1. Analysis: 01000001 >> 1 = 00100000 = 32

01000001 >> 1 = 00100000 = 32

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of “Python article” and we will get back to you.

Upcoming Batches For Python Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th March,2023

25th March

SAT&SUN (Weekend Batch)
View Details REGISTER FOR FREE WEBINAR Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month

### Subscribe to our Newsletter, and get personalized recommendations.  #### Everything You Need To Know About Bitwise Operators In Python

edureka.co 