Python Bitwise Operators – Start operating on the bits of numbers

Today, we are going to understand 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.

Before moving ahead, first get yourself acquainted with the Python operators.

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 takes two arguments and performs OR operation on each bit of the value of the operand.

In the OR operation in binary, the resultant is 0 when both operands are 0 otherwise it is 1. 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.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

Any doubts in Python bitwise operators till now? Mention them in the comment section.

4. Bitwise 1’s complement (~)

The bitwise 1’s complement only works on one operand. 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 the bitwise operators in Python. 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.

If you liked TechVidvan’s Python bitwise operators article, don’t forget to share our article on social media.

Happy Learning!!

1 Response

  1. adarsh sewani says:

    in python bitwise operators, the first bitwise (&) is explained in which u have written 19 & 7 will give us 5 but in binary chart its 3 ; first i don’t know how binary chart works and this example is making me more confused. plss help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.