Programs

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.

Operator Example Meaning
& a & b Bitwise AND
| a | b Bitwise OR
^ a ^ b Bitwise XOR (exclusive OR)
~ ~a Bitwise NOT
<< a << n Bitwise left shift
>> a >> n Bitwise 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.

Where 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.

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

Why 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.

Want to share this article?

Prepare for the Job of the Future

Leave a comment

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

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks