Date:

Share:

Python Tutorials – Episode 4 – Bitwise Operators and Operations

Related Articles

In previous Episode, you learnt how Arithmetic works in Python. In this Episode, we will talk about Python Bitwise Operators. These operators perform operations that they govern. These operations are called Bitwise Operations.

Bitwise operators and Bitwise Operations are a little complex for people who are completely new to programming. In order to keep all of you readers on the same page, I will also explain all the operators in their binary form as well. So that those who studied computer science in past but may have forgotten the concepts or those who are completely new to computer science may get on the same page. So lets begin.

What are Bitwise Operator?

The operations that you do on bits or string of bits to get desired output are called Bitwise Operations. The symbols that govern these operations are called Bitwise Operators.

In early versions of Python, Bitwise Operations were limited to bits in a processor. Thus, if your computer’s processor was 16-bit, then only 16-bits could be used to perform Bitwise Operations. Similarly, 32-bit processor could only perform on 32-bits and 64-bit processor on 64-bits. And to use Bitwise Operations, programmers needed to make sure that it ran on most of the processors. This, not only, made programming complex but also less portable as programmers needed to know the bit architecture of computers. And it started to void the statement that python was made for simplicity. Due to this, python has recently moved from processor based bits to infinite string of bits. Although not infinite technically, this string of bits can only go as big as your ram i.e. 2-Gigabyte ram will have 2*8-Gigabits -> 16-Gigabits.

These operations can be of various kinds but we will only talk about six different that fall in the scope of python to best of my knowledge.

I will also try to be as explanatory as possible. I will try to explain each of the six operations using following questions:

  • How does the operator work in Binary?
  • How does operator work in Python?

And finally I will try my best to explain/answer the most fundamental question most candidates ask. What the heck are uses of these Bitwise operators? Where are they mostly used?

So lets begin.

Left Shift Operator

Left Shift Operator (LSO) shifts the bits to left. for example you have a decimal number 12. Its binary will be

00001100

You want to move, for any reason, each bit’s position towards left by, say, 2 bits. After moving the bits leftward, your binary will look like this

00110000

Your decimal 12 after LSO will be 48. Go ahead try yourself and play with bits. So, how does this work in Python? I think this is the most easiest.

>>> 12 << 2
48

Lets create a small table and left shift in it using 8 bit string

Given NumberBinaryLeft Shift byLeft Shifted BinaryFinal Decimal
120000110020011000048
220001011020101100088
2300010111310111000184
1700010001310001000136
20000001050100000064

Now lets do the Python for all of the above Given Numbers

>>> 12 << 2
48
>>> 22 << 2
88
>>> 23 << 3
184
>>> 17 << 3
136
>>> 2 << 5
64

What happened here?

You may be wondering at a simple pattern here as why each answer to each number is even despite using odd numbers. The simplest way to say will be that each answer is multiple of input by ‘total number’ of possible combinations shift-by bits. For example, total number of possible combinations of 2-bits is 4 (00 01 10 11). When we shift 12 but 2-bits, it gives us an answer that is 4 times 12 i.e. 48 Now, if you are thinking that LSO may be used to multiply number at binary level, then you are right. That is one of the uses of the LSO.

Right Shift Operator

Right Shift Operator (RSO) shifts the bits to right. Here, we will use the same examples as in Left shift operator to make it more understandable for you. Lets use decimal number 12. Its binary is

00001100

You want to move, for any reason, each bit’s position towards right by 2 bits. After moving the bits rightward, your binary will look like this

00000011

Your decimal 12 after RSO will be 3. Go ahead try yourself and play with bits. So, how does this work in Python? I think this is the most easiest.

>>> 12 >> 2
3

Now, lets create a small table and right shift in it using 8 bit string

Given NumberBinaryRight Shift byRight Shifted BinaryFinal Decimal
12000011002000000113
22000101102000001015
23000101113000000102
17000100013000000102
2000000105000000000

Now lets do the Python for all of the above Given Numbers

>>> 12 >> 2
3
>>> 22 >> 2
5
>>> 23 >> 3
2
>>> 17 >> 3
2
>>> 2 >> 5
0

What happened here?

Again there is a pattern in RSO. Although it is not visible to untrained eye but you may see the pattern in it. As I said in LSO, 2-bits can be rearranged in only 4 different combinations. Similarly, 3-bits can be rearranged in 8 different combinations and 4-bits in 16 and 5-bits in 32 different combinations. Now, looking at 12, if you say that if LSO was multiplier, then answer is 3 because 12 is divided by 4. Then you are right. It is division. But all the remainders are ignored in this division. For example look at 23 and 17. Both are giving the answer 2 because highest multiple of 8 that is less than or equal to these two numbers is 16. Hence, in case of 23, remainder is 7 which is ignored and respectively in case of 17, remainder is 1 which is also ignored.

Coming to 2 which is right shifted by 5 bits. I have a question, why is answer to this 0, comment below your answer. All the right answers will be mentioned in hall of fame of the week.

Bitwise AND Operator

Lets first discuss what AND means in computer science. It is one of the basic computer science principles. In micro-electronics, a special combination of current flow through transistors creates the functionality of AND and is known as AND gate. Every computer that you know exists and that you do not know exists uses AND gate in one way or another.

What AND gate does is that it takes two input and gives an output only if both inputs are same. Taking this concept, an AND operator is created which takes two sets of bits and returns a set of bits in which only those bits that are 1 in both sets of input bits are 1 and all others are returned as 0.

For example, we take 9 and 12 from decimal numbers and convert them into binary we get 00001001 and 00001010 respectively. If we pass them through AND we get the answer 00001000 which equals to 8 in decimal numbers.

Lets do this in another way.

Given 9 and 12
Binary of 9  = 00001001
Binary of 12 = 00001010

Using Bitwise AND we keep 1 in only those places where both inputs have 1 and
convert all others to 0

  00001001
& 00001010
------------
  00001000
------------

This answer is equal to 8 in decimal number system

I hope I have done justice with explanation so far. If you have any questions, you are welcome to ask in comments. Lets do this  in a table.

Given First Number and its BinaryGiven Second Number and its BinaryOperation AppliedResulting BinaryResults in Decimal
1  = 000000012  = 00000010AND000000000
3  = 000000115  = 00000101AND000000011
7  = 0000011115 = 00001111AND000001117
11 = 0000101123 = 00010111AND000000113
14 = 0000111029 = 00011101AND0000110012
242 = 11110010249 = 11111001AND11110000240
129 = 01000010129 = 01000010AND01000010129

Now lets do the same in Python.

>>> 1 & 2
0
>>> 3 & 5
1
>>> 7 & 15
7
>>> 11 & 23
3
>>> 14 & 29
12
>>> 242 & 249
240
>>> 129 & 129
129

What happened here?

In first example where 1 and 2 are passed through and we get 0 as answer. Because there is no single place the two sets where 1 is on same position. In second example where 3 and 5 are used, we get 1 as answer because only 1 bit in all the places of both sets is at same place. Similarly, the rest examples are solved. If you find any mistakes or have any query, leave a comment below.

As you see, only the same bits are activated in the answer and rest are converted into 0. This operator is mostly used when comparing two sets of bits and if the result is cross checked against the inputs to make sure that output is same as inputs. If result is same as input, then two inputs are same. If result is not same as inputs, then two inputs are different.

Bitwise OR Operator

Next in Bitwise Operators is OR. OR is also one of the most basic computer science principles. Using this principle, another logic gate was invented and named OR gate. OR gate will only return 0 if both the inputs in OR gate are 0. OR gate will always return 1 if any of the inputs are 1

Using the same principle to create OR gate, OR operator is introduced in programming languages. Same is the case with Python. Two different numbers given as inputs and a new number is returned as output. This output works on the principle of OR.

For example, if we want to use OR on 2 and 5 manually on paper. Then first we will first take binary of 2 which is 010 and 5 which is 101. Then using principle of OR, each bit on each position in one number is compared against the bit on same position in second number. The resulting answer comes as 111 which is equal to 7 in decimal.

Lets do this in another way

Given 2 and 5
Binary of 2  = 00000010
Binary of 5  = 00000101

Using Bitwise OR we keep 0 in only those places where both inputs have 0 and
convert all others to 1

  00000010
| 00000101
------------
  00000111
------------

This answer is equal to 7 in decimal number system

Lets do same in a table using 8 bit string

Given First Number and its BinaryGiven Second Number and its BinaryOperation AppliedResulting BinaryResult in Decimal
2 =   000000105 =   00000101OR000001117
9 =   000010016 =   00000110OR0000111115
7 =   000001118 =   00001000OR0000111115
8 =   000010006 =   00000110OR0000111014
9 =   000010014 =   00000100OR0000110113

You may be thinking that OR must me doing addition in Binary. Yes, you are right to think that. But do not be fooled with the selected examples. These examples presented above were intentionally created to make you think so.

Lets have a look at other examples to help clear the doubts using 8-bits.

Given First Number and its Binary Given Second Number and its Binary Operation Applied Resulting Binary Result in Decimal
6 =   000001105 =   00000101OR000001117
8 =   000010009 =   00001001OR000010019
11 = 0000101112 = 00001100OR0000111115
74 = 0100101043 = 00101011OR01101011107

Lets do the above examples in Python

>>> 2 | 5
7
>>> 9 | 6
15
>>> 7 | 8
15
>>> 8 | 6
14
>>> 9 | 4
13
>>> 6 | 5
7
>>> 8 | 9
9
>>> 11 | 12
15
>>> 74 | 43
107

What happened here?

In the first example, OR is applied to 2 and 5. The result is 7 because the available 0-bits in binary of 5 replaced by 1-bits in 2. Since there are no overlapping 1-bits in binary of 2 and 5, the result seems like addition. Similarly, when we take into consideration application of OR on 6 and 5, we see the same result. This is because there are overlapping 1-bits in binary of both 6 and 5 which results as 1 due to or operation.

If you think, you need to know more on how OR works, let me know in comments.

Complement of Number

Complement of Number is the method of Mathematics and Computer Sciences. This method is used to subtract one number from another using only addition of positive integers. So, lets dive in.

Bitwise XOR Operator

XOR also known as Exclusive OR. This operator also takes two inputs but its inner workings are complicated due to the fact that this operator is combination of Bitwise AND, Bitwise OR and Bitwise Compliment internally. I will also explain the internals of XOR in the end of this topic.

Using combination of Bitwise Operations

Conclusion on Python Bitwise Operators and Operations

These were six bitwise operators and their related operations. If you have any query regarding bitwise operators or bitwise operations, comment bellow and I will edit the post with your queries answered.

2 COMMENTS

  1. An impressive share! I have just forwarded this onto a friend who was conducting a little research on this. And he in fact bought me dinner due to the fact that I discovered it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending time to discuss this subject here on your site.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Popular Articles