Examples of using bitwise operators in Python
By Steve Claridge on 2023-09-20.
This article describes how to manipulate binary numbers (bit twiddling) using Python, or in other words: how to modify individual bits in a number. Why would you want to modify a single bit when setting a number like
a = 24
is easy and just works? You probably won't need to twiddle bits, if you are writing web app or other higher-level type of application. If you are writing something lower-level, where data size and speed is important, you might want to store things as small as possible. For example, Bluetooth, cell phone connectivity and other data transfer specifications set flags that are 1 bit in size, this allows 8 flags to be set to yes/no (1 or 0) within a single 8-bit byte. In other words, by manipulating at the bit-level you can use the smallest amount of memory possible to store the data you want.
The following piece of code will be repeated often throughout this page. It prints a number in its usual decimal format and then prints it as a binary string
a = 9
print(a)
print("{:b}".format(a))
Running this will print the numbers below, if you are not sure why 1001
is the binary representation of 9
then have a look at this video explainer.
9
1001
Shifting left
Looking at the number 9 again in binary form, we can see it's 1 + 8 = 9
8 4 2 1
1 0 0 1
Lets shift the binary digits to the left by 1, the same 1001
pattern is still there, but it's moved to the
left and a 0 has been added to the right.
16 8 4 2 1
1 0 0 1 0
So now the number is a 16 + 2 = 18, we've doubled the number by shifting left once. The Python code to shift
left is <<
, for example on line two here
a = 9
a = a << 1
print(a)
print("{:b}".format(a))
You can of course shift left by more than one place 9 << 2
equals 36. 1 << 10
equals 1024.
Shifting right
If you've grokked the shift left above then this bit will be easy, its exactly the same idea but bits move to the right, lets start with the number 22
16 8 4 2 1
1 0 1 1 0
If we >> 1
shift this number right then it now looks like this
a = 22
a = a >> 1
16 8 4 2 1
0 1 0 1 1
8 + 2 + 1 = 11. Let's see examples starting with number 4, note than you can shift 'off the end' and end up with zero
4 2 1
1 0 0
4 >> 1 = 2 (0 1 0)
4 >> 2 = 1 (0 0 1)
4 >> 3 = 0 (0 0 0)
4 >> 27 = 0 (0 0 0)
AND two numbers together
This is a bit different to the above, we aren't moving bits to the left or right, instead we are going to take
two numbers and AND
them together. Our input numbers are 7 and 2
4 2 1
7 = 1 1 1
4 2 1
2 = 0 1 0
Look at the two numbers in their binary form as see which bits they have set to 1. Now, when we AND 7 and 2 we are saying 'produce a new number that has bit(s) set to 1 where the first input number AND the second input number both have the bit set to 1'
1 1 1
0 1 0
-----
0 1 0
So 7 AND 2 = 2
, in Python this is written using a single ampersand
a = 7 & 2
print(a)
print("{:b}".format(a))
OR you could do this
Similar to ANDing two numbers together, you can OR
them, which is to say 'produce a new number that has bit(s)
set to 1 where either the first input number OR the second input number have the bit set to 1'
1 1 0
0 1 0
-----
1 1 0
Python uses the single pipe for this, so
a = 6 | 2
b = 0 | 0
c = 2198212 | 19191