Data Science and Machine Learning Internship ...
- 22k 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.
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.
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.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 26th October,2024 26th October SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 30th November,2024 30th November SAT&SUN (Weekend Batch) | View Details |
edureka.co