Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPython Bitwise Operators [With Examples]

Python Bitwise Operators [With Examples]

Last updated:
10th Sep, 2022
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Python Bitwise Operators [With Examples]

Bitwise operators are provided by the Python programming language to allow programmers to manipulate data at the most basic level – giving them a lot more control over all the variables. 

Python is one of the most popular programming languages in today’s world. Python’s versatility and the number of features it offers are a few reasons for its increasing popularity. This article will explore Bitwise operators in Python, different types of bitwise operators, their uses, and much more.

Overview of Python’s bitwise operators

Bitwise operators look the same across different programming languages. Python comes with various types of operators like arithmetic, logical, and comparison operators.

OperatorExampleMeaning
&a & bBitwise AND
|a | bBitwise OR
^a ^ bBitwise XOR (exclusive OR)
~ ~aBitwise NOT
<<a << nBitwise left shift
>>a >> nBitwise right shift

Most of the bitwise operators are binary, which means that they need two operands to work with, typically referred to as left and right operand. Bitwise NOT (~) is the only unary bitwise operator since it needs only one operand.

Explore our Popular Data Science Courses

What are Bitwise operators?

Bitwise operators are one of the types of operators used to perform bitwise calculations on integers. The integers are first converted into binary and then the operations are performed bit by bit hence, the name bitwise operators. The result is then turned into decimal format. These operators can perform calculations only on integers.

Check out our data science certifications to upskill yourself.

The importance of bitwise operators

You probably understood what bitwise operators are, now let’s focus on the importance of bitwise operators. They are better than arithmetic operators and here’s why you need to use them:

  • Execution speed

Bitwise operators are faster than performing multiplication or division. If you need to multiply a variable x by say 9, you can do (x<<3 + x) which would be a few cycles faster than (x*9).

  • Error checking

Bitwise operators are used extensively for checking errors. If you are sending out some bits to another computer on another server, there are bound to be a few errors. You can identify these errors using bitwise operators. 

You probably understand the use and importance of bitwise operators, so, let’s move on to understand the different types of bitwise operators.

Different types of bitwise operators

Bitwise Logical Operators

The bitwise operators and, or, and xor are binary operators that have a left and right operand. The bitwise operator is not a unary operand that has only a right operand. The result type of all four bitwise operators is integers. The bitwise logical operators examine one bit at a time in their operand before and compute the corresponding value in the result.

Bitwise AND

The AND (&) operator is used to perform an AND between two bits. In this case, both bits need to be true for the output to be true. If either of the bit is false, the output is false. In other words, if both the bits are 1, it returns 1 else 0.

1&1 = 1

1&0 = 0

0&0 = 0

0&1 = 0

Example:

       a = 10 = 1010 (Binary)

b = 4 =  0100 (Binary)

       

      a & b = 1010

             &

            0100

      = 0000

      = 0 (Decimal)

Bitwise OR

The OR (|) operator is used to perform the OR operation between two bits. In order for the output to be true, one of the bits needs to be true. Unlike the (&) operator, both the bits need not be true for the result to be true. In other words, the resultant bit is 0 if and only if both the bits in considerations are 0 else the result is 1.

1 | 1 = 1

1 | 0 = 1

0 | 1 = 1

0 | 0 = 0

Example:

a = 10 = 1010 (Binary)

b = 4 =  0100 (Binary)

a | b = 1010

            |

          0100

      = 1110

      = 14 (Decimal)

Bitwise NOT

Bitwise NOT (~) operator is used for returning the complement of the number in Python. However, the not operator is a very simple operator and is used to flip the bit from 0 to 1 and from 1 to 0. 

~13  (0b01101) =  -14

~16 (0b010000) =  -17

~31 (0b011111) = -32

Example:

a = 10 = 1010 (Binary)

     ~a = ~1010

   = -(1010 + 1)

   = -(1011)

   = -11 (Decimal)Bitwise XOR Operator

The XOR (^) operator is used to XOR between two bits that are under operation. XOR operation is very simple. If two bits are the same then the resulting bit is 0 else 1. It is widely used in logical calculations to make the process faster.

 

1 ^ 1 = 0

1 ^ 0 = 1

0 ^ 1 = 1

0 ^ 0 = 0

Example:

a = 10 = 1010 (Binary)

b = 4 =  0100 (Binary)

       a & b = 1010

             ^

            0100

      = 1110

      = 14 (Decimal)

Shift Operators

These bitwise operators are used to shift the bits of a number from left to right by multiplying or dividing the number by two respectively. They can be used when we have to divide or multiply the number by two.

Bitwise Right Shift

The right (>>) operator, as the name suggests, shifts the bits towards the right to a number represented to the right side of the operator. 

Read our popular Data Science Articles

For example – 10>>2 will shift (1010) towards the right by 2.

Example 1:

a = 10 = 0000 1010 (Binary)

a >> 1 = 0000 0101 = 5

Example 2:

a = -10 = 1111 0110 (Binary)

a >> 1 = 1111 1011 = -5

Bitwise Left Shift

The left (<<) operator, as the name suggests, shifts the bits towards the left to a number represented on the right side of the operator.

For example – 1<< 2 will shift 1 towards left for 2 values.

Example 1:

a = 5 = 0000 0101 (Binary)

a << 1 = 0000 1010 = 10

a << 2 = 0001 0100 = 20 

Example 2:

b = -10 = 1111 0110 (Binary)

b << 1 = 1110 1100 = -20

b << 2 = 1101 1000 = -40

Read our popular Data Science Articles

Conclusion

Having a good understanding of python bitwise operators will help you to manipulate binary data in your projects. You now understand the basics of bitwise operators and their syntax and the data types that support them. You can also customize their behavior for your own needs.

If you are eager to learn and expand your knowledge about python and bitwise operators, check out upGrad’s & University of Maryland’s Professional Certificate Program in Data Science and Business Analytics, which is created for working professionals that offer 20+ case studies and assignments that can be added to your portfolio, 100+ hours of live sessions, 400+ hours of learning with job assistance from top firms.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1Where are bitwise operators used?

Bitwise operators can be used to manipulate individual bits of a number. In Python, bitwise operators perform bitwise calculations on integers. First, integers are converted into binary, and then operations are performed in small chunks - this is how bitwise operators got their name. Python bitwise operators are used exclusively on integers, returning results in decimal format.

2What is a single operand operator?

Arithmetic operators perform mathematical operations such as addition and subtraction with operands. Unary and binary are the two categories of mathematical operators. Unary operators perform a function with a single operand, e.g.: Bitwise NOT (~), whereas binary operators use two operands.

3Why do we need bitwise operators?

Bitwise operators are a great way to efficiently use space when representing data. These operators are necessary when operating on data provided by the hardware where a specific bit in a world is meaningful. Bitwise operators are necessary to save memory. Packing data into bits instead of words saves memory, and often you might have huge amounts of data and limited amounts of memory.

Explore Free Courses

Suggested Blogs

What is Linear Data Structure? List of Data Structures Explained
49860
Data structures are the data structured in a way for efficient use by the users. As the computer program relies hugely on the data and also requires a
Read More

by Rohit Sharma

30 Nov 2023

KDD Process in Data Mining: What You Need To Know?
46813
As a working professional, you are familiar with terms like data, database, information, processing, etc. You must have also come across terms like da
Read More

by Rohit Sharma

30 Nov 2023

Top 7 Data Types of Python | Python Data Types
96887
Data types are an essential concept in the python programming language. In Python, every value has its own python data type. The classification of dat
Read More

by Rohit Sharma

30 Nov 2023

Searching in Data Structure: Different Search Methods Explained
33241
The communication network is expanding, and so the people are using the internet! Businesses are going digital for efficient management. The data gene
Read More

by Rohit Sharma

30 Nov 2023

How to Implement Switch Case Functions in Python? [2023]
113090
Introduction Have you ever wondered if there is an alternative to write those complex If-else statements in Python? If you do not want multiple ‘If’
Read More

by Rohit Sharma

30 Nov 2023

Binary Tree in Data Structure: Properties, Types, Representation &#038; Benefits
77332
Data structures serve as the backbone of efficient data organization and management within computer systems. They play a pivotal role in computer algo
Read More

by Rohit Sharma

30 Nov 2023

Top 10 Business Intelligence Interview Questions and Answers [For Beginners &#038; Experienced]
16089
Introduction When you buy a product from an e-commerce site, you will often find that you will be asked to review the product and provide feedback on
Read More

by Rohit Sharma

29 Nov 2023

10 Exciting Python GUI Projects &#038; Topics For Beginners [2023]
54594
Python GUI projects offer a great way to become proficient in Python programming, and they include a wide range of exciting options, including Python
Read More

by Rohit Sharma

27 Nov 2023

17 Must Read Pandas Interview Questions &amp; Answers [For Freshers &#038; Experienced]
50409
Pandas is a BSD-licensed and open-source Python library offering high-performance, easy-to-use data structures, and data analysis tools. Python with P
Read More

by Rohit Sharma

04 Oct 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon