Python Bitwise Operators with Syntax and Example
1. Python Bitwise Operators
Previously, in our tutorial on Python Operators., Today, in this Python Bitwise Operators Tutorial, we will discuss Python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operators in Python Programming. Along with this, we will discuss syntax and example of Python Bitwise Operators.
So, let’s start the Python Bitwise Operators Tutorial.
2. Introduction to Python Bitwise Operators
Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole. To take an example, let’s see the ‘and’ and ‘&’ operators for the same thing.
Let’s take two numbers- 5 and 7. We’ll show you their binary equivalents using the function bin().
Now let’s try applying ‘and’ and ‘&’ to 5 and 7.
>>> 5 and 7
You would have expected them to return the same thing, but they’re not the same. One acts on the whole value, and one acts on each bit at once.
Actually, ‘and’ sees the value on the left. If it has a True Boolean value, it returns whatever value is on the right. Otherwise, it returns False. So, here, 5 and 7 is the same as True and 7. Hence, it returns 7. However, 5&7 is the same as 101&111. This results in 101, which is binary for 5. Let’s look at each of these operators bit by bit (pun intended).
Let’s move ahead with next Python Bitwise Operator
3. Python Bitwise AND (&) Operator
1 has a Boolean value of True, and 0 has that of False. Take a look at the following code.
This proves something. Now, the binary and (&) takes two values and performs an AND-ing on each pair of bits. Let’s take an example.
>>> 4 & 8
Binary for 4 is 0100, and that for 8 is 1000. So when we AND the corresponding bits, it gives us 0000, which is binary for 0. Hence, the output.
The following are the values when &-ing 0 and 1.
Table.1 Python Bitwise Operators – AND Operators
|0 & 0||0|
|0 & 1||0|
|1 & 0||0|
|1 & 1||1|
As you can see, an &-ing returns 1 only if both bits are 1.
You cannot, however, & strings.
Traceback (most recent call last):
File “<pyshell#30>”, line 1, in <module>
TypeError: unsupported operand type(s) for &: ‘str’ and ‘str’
Since Boolean values True and False have equivalent integer values of 1 and 0, we can & them.
Let’s try a few more combinations.
Traceback (most recent call last):
File “<pyshell#36>”, line 1, in <module>
TypeError: unsupported operand type(s) for &: ‘float’ and ‘float’
You can also type your numbers directly in binary, as we discussed in section 6a in our Python Numbers tutorial.
>>> 0b110 & 0b101
Here, 110 is binary for 6, and 101 for 5. &-ing them, we get 100, which is binary for 4.
4. Python Bitwise OR (|) Operators
Now lets discuss Python Bitwise OR (|) Operator
Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.
Table.2 Python Bitwise Operators – OR Operators
This is the same as the following.
Let’s see some more examples.
Lets move to another Python Bitwise Operator
5. Python Bitwise XOR (^) Operator
XOR (eXclusive OR) returns 1 if one operand is 0 and another is 1. Otherwise, it returns 0.
Table.1 Python Bitwise Operators – XOR Operators
Let’s take a few examples.
Here, this is the same as 0b110^0b110. This results in 0b000, which is binary for 0.
This is equivalent to 0b110^0b000, which gives us 0b110. This is binary for 6.
Here, 0b110^0b011 gives us 0b101, which is binary for 5.
Now lets discuss Bitwise 1’s Complement (~)
6. Bitwise 1’s Complement (~)
This one is a bit different from what we’ve studied so far. This operator takes a number’s binary, and returns its one’s complement. For this, it flips the bits until it reaches the first 0 from right. ~x is the same as -x-1.
To make it clear, we mention the binary values of both. Another example follows.
7. Python Bitwise Left-Shift Operator (<<)
Finally, we arrive at left-shift and right-shift operators. The left-shift operator shifts the bits of the number by the specified number of places. This means it adds 0s to the empty least-significant places now. Let’s begin with an unusual example.
Here, True has an equivalent integer value of 1. If we shift it by two places to the left, we get 100. This is binary for 4.
Now let’s do it on integers.
10 shifted by one place to the left gives us 100, which is, again, 4.
Now, 11 shifted to the left by two places gives us 1100, which is binary for 12.
Now lets move to Next Python Bitwise Operator
8. Python Bitwise Right-Shift Operator (>>)
Now we’ll see the same thing for right-shift. It shifts the bits to the right by the specified number of places. This means that those many bits are lost now.
3 has a binary value of 11, which shifted one place to the right returns 1. But before closing on this tutorial, we’ll take one last example.
Let’s check what’s the decimal value for 11111.
Now, let’s shift it three places to the right.
As you can see, it gives us 3, whose binary is 11. Makes sense, doesn’t it?
This was all about the Python Bitwise Operators.
While they’re not so common in real-world programming, Python Bitwise Operators do find their use in places like encryption, compression, and byte manipulation. If you have any doubts regarding Python Bitwise Operators, leave them in the comments.
Hope you like the Python Bitwise Operators.