## Python Certification Training for Data Scienc ...

- 83k Enrolled Learners
- Weekend/Weekday
- Live Class

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,

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!

Moving on with this article on 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.

Moving on with this article on Bitwise Operators In Python,

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

- &: Known as Bitwise AND. Example, X & Y = 0000.
- ^: Known as Bitwise exclusive OR. Example, X ^ Y = 1110.
- |: Known as Bitwise OR. Example, X | Y = 1110.
- ~: Known as Bitwise complement. Example, ~X = 00001001.
- <<: Known as Shift Left. Example, X << 1 = 00001100. Here the bits will be moved 1 step to the left.
- >>: 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

Moving on with this article on Bitwise Operators In Python,

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.

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

01000001 >> 1 = 00100000 = 32

This brings us to the end of this article.

*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.*

Comments 0 Comments

REGISTER FOR FREE WEBINAR

Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP