Python Bitwise Operators – Start operating on the bits of numbers

Today, we are going to understand python bitwise operators.

Python bitwise operators include And, Or, Xor, 1’s complement, Left-shift, and Right-shift. These operators are called bitwise because they operate on bits of the number. A number in binary is represented in the form of zeroes ‘0’ and ones ‘1’.

For example: the number 1234 in binary is represented as ‘10011010010’.

The bitwise operators work on these stream of bits which means that they work directly at the binary level. It may sound scary, but actually, it’s very easy to understand once you know how binary numbers are represented.

Types of Python Bitwise Operators

We have the following bitwise operators that are supported by Python –types of biwise operators in python

Let us go through each of them in a little bit more detail.

1. Bitwise And (&)

The bitwise AND operator takes two arguments and performs AND operation on the operands bit by bit. First, let’s recall how And operation on binary digits are implemented.implementation of bitwise AND operation

As you can observe, the operation will only be 1 when both operands are 1 otherwise it remains 0.

Example:

19 & 7 will give us 5.example of python bitwise operators

2. Bitwise Or (|)

The bitwise OR operator performs a logical OR operation on each bit of the operands. It takes two binary numbers and compares their corresponding bits. If either bit is 1, the result is 1; if both bits are 0, the result is 0. This operator is useful for setting specific bits in a number or combining flags and binary masks. For example, if you use bitwise OR to combine multiple binary flags, each bit in the result represents the presence of one or more flags.

Here’s a truth table for it.Python bitwise OR operators

Example:

12 | 9 will give us 13.example of bitwise OR operator

3. Bitwise Xor (^)

The bitwise Xor also takes two operands and performs Xor operation on the binary digits. The Xor operation gives 0 as a result when both operands are the same and 1 when operands are different. This means it effectively toggles the bits, flipping 1s to 0s and 0s to 1s, wherever the operands differ.bitwise Xor operators in Python

Example:

8 ^ 10 will give us 2.bitwise Xor example

One property of bitwise Xor is that it will return 0 only if both the numbers are the same.

5^5 = 0

128^128 = 0

4. Bitwise 1’s complement (~)

The bitwise 1’s complement operator works on a single operand and inverts all the bits of the number. This means that each 0 in the binary representation is changed to 1, and each 1 is changed to 0. It effectively flips every bit in the binary representation of the number, which is useful for operations like calculating the binary negation of a number. It is used to get the 1’s complement of a number. Let’s see the example of how 1’s complement works.

~5 = -6

It flips all the bits until it reaches first zero from the right, so ~x is the same as -(x+1)

~10 = -11

~258 = -259

5. Left-Shift (<<)

The left shift operator shifts the bits of the numbers by a specified number of places. It adds zeroes to the empty least significant places.

Syntax :

digit << number_of_places_to_shift

This will be better understood with the example.

Example 1:

1<<2 = 4

1 is represented as ‘1’

We shift the digits to left up to two places. So, it will be ‘100’ in binary and 4 in decimal.

Example 2:

10<<4 = 160

10 in binary is ‘1010’

Shifting 4 digits to the left will make it ‘1010 0000’ which is 160 in decimal.

6. Right-Shift (>>)

This is similar to the left shift, but instead of shifting digits to the left we are shifting them to the right. In this process, some of the bits are lost.

Example 1:

10>>2 = 2

Let’s see how this works.

So, 10 in binary is “1010”.

By shifting “1010” to 2 places to the right we will lose our 2 rightmost digits and we will only be left with “10”.

In decimal, “10” represents 2 and that’s our answer.

Example 2:

25>>2 = 6

25 in binary is “11001”. We right shift them up to 2 places so, we are now left with “110” which is 6.

Summary

That was all about TechVidvan’s python bitwise operators. Usually, they are not used in normal programming but in some cases, they are very useful like bit manipulations, encryptions, and compressions.

Operations on Bit operators are faster than the other operators.

Knowing how to use them effectively will make you a better programmer.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.