Skip to content
Home » Bitwise Operators in Python

Bitwise Operators in Python

Spread the love

Today we’ll be talking about bitwise operators in Python.

Here’s the video version of the article:

Bitwise operators work on bits. We can represent numbers in binary notation. Each digit (0 or 1) corresponds to 1 bit. For example the number 237 in binary notation is 11101101 and the number 49 in binary notation is 110001, or 00110001 to match the number of digits in the first number.

You can read more about binary numbers in my previous article. If you are interested in other types of numbers, like octal or hexadecimal, you can also read one of my older articles, where I cover decimal, binary, octal and hexadecimal literals.

You can easily obtain the binary representation of a decimal integer by using the bin function:

>>> bin(237)
'0b11101101'

>>> bin(49)
'0b110001'

We use bitwise operators for example in IoT applications where data is read from the sensors and specific actions are performed based on whether a specific bit or group of bits are set or not.

There are the following bitwise operators:

bitwise operators

Binary AND

Our example is 237 & 49.

If there’s 1 in both operands at the same bit position, there’s also 1 in the result. Otherwise there’s 0 in the result:

Binary AND

So, in this case the result is 00100001, which in decimal notation is:

>>> int(0b00100001)
33

And using the bitwise operator we have the same result:

>>> 237 & 49
33

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Binary OR

Our example is 237 | 49.

If there’s 1 in either operand at the same bit position, there’s also 1 in the result. There’s 0 in the result only if there’s 0 in both operands:

Binary OR

So, in this case the result is 11111101, which in decimal notation is:

>>> int(0b11111101)
253

And using the bitwise operator we have the same result:

>>> 237 | 49
253

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Binary XOR (Exclusive OR)

Our example is 237 ^ 49.

We have 1 in the result if there is 1 in one operand but not in the other:

Binary XOR

So, in this case the result is 11011100, which in decimal notation is:

>>> int(0b11011100)
220

The bitwise operator again gives us the same result:

>>> 237 ^ 49
220

Binary Left Shift

Our example is 237 << 2.

The operand on the left is shifted left the number of bits specified by the operand on the right:

Binary Left Shift

As the bits move to the left, zeros appear on the right. So, in this case the result is 1110110100, which in decimal notation is:

>>> int(0b1110110100)
948

Let’s see:

>>> 237 << 2
948

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Binary Right Shift

Our example is 237 >> 2.

The operand on the left is shifted right the number of bits specified by the operand on the right:

Binary Right Shift

As the bits move to the right, zeros appear on the left and the bits farthest to the right are cut off. So, in this case the result is 111011, which in decimal notation is:

>>> int(0b111011)
59

And here’s the result we get with the operator:

>>> 237 >> 2
59

Power of Two Multipliers

The shift operators are an easy way to multiply or divide a number by a power of two. Shifting n bits to the left is equivalent to multiplying by 2 raised to the power of n:

>>> 237 << 2
948
>>> 237 * 2 ** 2
948

>>> 24 << 4
384
>>> 24 * 2 ** 4
384

Shifting n bits to the right is equivalent to dividing (using floor division) by 2 raised to the power of n:

>>> 648 >> 5
20
>>> 648 // 2 ** 5
20

Augmented Assignment Statements

You can also use augmented assignment statements which combine bitwise operators with the basic assignment operator. So, if we have something like a = a & b, the shorthand for this is a &= b. What this statement does is perform the bitwise AND  operation and assign the result back to a.

Here are the augmented assignment statements and the full forms corresponding to them:

Augmented Assignment Statements

Spread the love

Leave a Reply