Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

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

Updated on 21 December, 2023

6.03K+ views
8 min read

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.

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)

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

2. Bitwise Shift Operators

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

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!

Frequently Asked Questions (FAQs)

1. What 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.

2. Are 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.

3. What 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