Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconWhat Do You Need to Know About Python Bitwise Operators? [Explained with Examples]

What Do You Need to Know About Python Bitwise Operators? [Explained with Examples]

Last updated:
6th Oct, 2021
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
What Do You Need to Know About Python Bitwise Operators? [Explained with Examples]

Computer systems represent, store, and transmit data as a stream of binary digits called bits. ‘Binary’ means that there can be only two possible values, 0 and 1, and each such binary digit is known as a ‘bit.’ Whether you work with text, decimal numbers, images, sounds, or videos, computer software translates the information into binary code comprising 0s and 1s. Often referred to as machine language, binary information is the most fundamental level of information a computer system stores.

Bitwise operators in Python lets us manipulate individual bits of data at the most basic level. In other words, bitwise operators in Python are used to perform bitwise calculations on integer values. Thus, the integer values are first converted into binary form, and then bit-by-bit operations are performed. After the ‘bitwise operations’ are done, the results are returned in decimal format. Bitwise operators in Python work only on integers. Upskilling yourself with data science programs will help you overcome the challenges. Let’s talk more about text mining.   

Now, let’s explore Python bitwise operators in more detail, the types, how they work, and examples.

Bitwise Operators in Python

Python includes six operators for performing bitwise logical operations on integers. 

The following are the different bitwise operators in Python with their syntax and a description of what each does.

Operator

Syntax

Description

Bitwise AND 

a & b  

The operator returns 1 if both bits in the operands are 1. Else returns 0.

Bitwise OR

a | b

The operator returns 1 if either bit in the operands is 1. Else returns 0.

Bitwise NOT

~a

A unary bitwise operator that performs logical negation of a given number by flipping all its bits. Arithmetically, it is expressed as subtracting individual bit values from one (~ai = 1 – ai).

Bitwise XOR 

a ^ b

The operator returns 1 if a bit pair contains opposing bit values. Else returns 0.

Bitwise Left Shift

a << 

The operator shifts the bits of its first operand to the left by the number of places defined in the second operand. In addition, the operator adds enough 0s to fill the gap that forms on the right side of the new bit pattern.

Bitwise Right Shift

a >> 

The operator pushes the bits to the right by the specified number of places. Thus, the rightmost bits are always dropped.

Explore our Popular Data Science Courses

Bitwise Operators in Python With Examples

With the basic description of each bitwise operator in mind, let us look at some examples to clarify the concept further.

1. Bitwise AND (&)

Returns 1 if both the bits are 1. Else returns 0.

  • Example #1

a = 24 = 11000 (Binary)

b = 4 = 00100 (Binary)

a & b = 11000

                  &

               00100

           = 00000

           = 0 (Decimal)

  • Example #2

a = 10 = 1010 (Binary)

b = 2 = 0010 (Binary)

a & b = 1010

                 &

              0010

           = 0010

           = 2 (Decimal)

Top Essential Data Science Skills to Learn

2. Bitwise OR ( | )

Returns 1 if either of the bits is 1. Else returns 0.

  • Example #1

a = 67 = 1000011 (Binary)

b = 54 = 0110110 (Binary)

a | b = 1000011

                   |

             0110110

         = 1110111

         = 119 (Decimal)

  • Example #2

a = 6 = 000110 (Binary)

b = 34 = 100010 (Binary)

a | b = 000110

                 |

            100010

         = 100110

         = 38 (Decimal)

Our learners also read: Free Online Python Course for Beginners

3. Bitwise NOT (~)

Returns 1’s complement of the bit (inverts the bit).

  • Example #1

a = 60 = 111100 (Binary)

~a = ~111100

      = 000011

      = 3 (Decimal)

  • Example #2

a = 33 = 100001 (Binary)

~a = ~100001

      = 011110

      = 30 (Decimal)

4. Bitwise XOR (^)

Returns 1 if both the bits have opposing values. Else returns 0.

  • Example #1

a = 61 = 0111101 (Binary)

b = 90 = 1011010 (Binary)

a ^ b = 0111101

                    ^

              1011010

          = 1100111

          = 103 (Decimal)

  • Example #2

a = 2 = 000010 (Binary)

b = 55 = 110111 (Binary)

a ^ b = 000010

                   ^

              110111

          = 110101

          = 53 (Decimal)

5. Bitwise Left Shift (<<)

Shifts the bits to the left and fills the gaps formed on the right with 0.

  • Example #1

a = 39 = 100111 (Binary)

a << 1 = 1001110 = 78 (Decimal)

a << 2 = 10011100 = 156 (Decimal)

a << 3 = 100111000 = 312 (Decimal)

  • Example #2

b = 74 = 1001010 (Binary)

b << 1 = 10010100 = 148 (Decimal)

b << 2 = 100101000 = 296 (Decimal)

b << 3 = 1001010000 = 592 (Decimal)

upGrad’s Exclusive Data Science Webinar for you –

How upGrad helps for your Data Science Career?

 

6. Bitwise Right Shit (>>)

Shifts the bits to the right and fills the gaps formed on the left with 0.

  • Example #1

a = 157 = 10011101 (Binary)

a >> 1 = 1001110 = 78 (Decimal)

a >> 2 = 100111 = 39 (Decimal)

a >> 3 = 10011 = 19 (Decimal)

  • Example #2

b = 89 = 1011001 (Binary)

b >> 1 = 101100 = 44 (Decimal)

b >> 2 = 010110 = 22 (Decimal)

b >> 3 = 001011 = 11 (Decimal)

Bitwise Operators in Python (Examples With Code)

Following are some simple code snippets to illustrate bitwise operators in Python:

1. Bitwise AND, OR, NOT, and XOR

Source

Source

2. Bitwise Shift Operators

Source

Source

Way Forward: Master Python Skills with upGrad’s Professional Certificate Program in Data Science

Are you an aspiring Business Manager with almost no experience in coding? Then upGrad has got you covered with the Professional Certificate Program in Data Science for Business Decision Making.

The Professional Certificate Program in Data Science is specially designed for managers and working professionals who want to develop the skills and practical knowledge in foundational data science. The rigorous, 8-months online program focuses on data science problems, algorithms, and how data can be presented to take important business decisions. The curriculum covers the following courses:

  • Pre-program preparatory content
  • Business problem solving, insights, and storytelling
  • Statistics and machine learning
  • Strategy
  • Capstone project
  • Introduction to Python (Optional module)

Here’s a look into the key highlights of the program:

  • Globally recognised certification from IIM Kozhikode
  • 200+ hours of content, 20+ live learning sessions, 5+ expert coaching sessions
  • 3 industry projects and a capstone
  • Comprehensive coverage of Excel, Python, Tableau, R, and Power BI
  • 360-degree career support
  • One-on-one with industry mentors
  • Job assistance with top firms
  • Industry networking opportunities

Read our popular Data Science Articles

Conclusion

Python is a powerful, versatile, easy-to-use programming language, making it a popular choice among experts and beginners alike. Besides, Python is still the programming language of choice among developers due to its sheer efficiency, versatility, reliability, and speed. It can be used in nearly all kinds of environments and platforms without any significant performance loss. In addition, Python powers the bulk of data processing workloads across many organisations. The ease of data analysis that Python offers renders it useful for many applications, including machine learning projects.

Check out All Python tutorial concepts Explained with Examples.

Python is undoubtedly handy for tackling critical business processes since it forms a fundamental aspect of data science. Further, as Big Data expands and more and more enterprises focus on making data-driven decisions, the importance of Python cannot be undermined. So, if you want to master Python and apply your skills in data-driven business scenarios, sign up with upGrad to learn from the best in the industry!

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)

1What are bitwise operators used for?

Bitwise operators in Python are used to perform bitwise calculations on integer values. For this, the integers are first transformed into binary, and then various operations are performed on, bit by bit, returning the resulting in decimal format. In Python, bitwise operators can be used only on integers.

2Are bitwise operators fast?

On a simple, low-cost processor, bitwise operators are typically faster than division, multiplication, and often substantially faster than addition. In general, bitwise operators use fewer resources and hence use significantly less power.

3What is the use of logical operators in Python?

Python has three logical operators that allow comparing values. The three logical operators AND, OR, and NOT evaluate expressions to Boolean values and return True or False based on the operator’s outcome. Given below are the three logical operators in Python:

1. AND ( a and b): True if both expressions are true
2. OR (a or b): True if at least one expression is true
3. NOT (not a): True only if the expression is false

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905263
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions &#038; Answers [For Freshers &#038; Experienced]
20923
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5068
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5179
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17645
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types &#038; Techniques
10803
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80766
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories &#038; Types [With Examples]
139134
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

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