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.

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)

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)

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

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.

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!

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.

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.

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

Prepare for the career of future

0 replies on “What Do You Need to Know About Python Bitwise Operators? [Explained with Examples]”

×