- Podcasts
- Python To You
- _14 Python Tutorial for Beginners _ Python BitWise Operators
_14 Python Tutorial for Beginners _ Python BitWise Operators
Python To You
- October 16, 2026
- 13 min
About this episode
No description.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
_14 Python Tutorial for Beginners _ Python BitWise Operators
We have talked about assignment operator, arithmetic operators and logical operators. Now it's time to focus on some more operators and this time we are going to work with Bitwise. Now if you remember in one of the videos we have talked about binary formats, right? So this is where you will use them. So for sure the name suggests it is something to do with binary formats, right? So whatever you're going to do now is with bits. So let's get started. So let's open Python and let's do this calculation. Now the first one. So if we talk about bitwise, we have six different operators. We have complement operator, then we have and, or, xor, left shift and right shift. Okay, let's start with the first one, which is complement operator. And if you look at the symbol of it, it's weird, right? We don't use this symbol much. So this is called as tilde operator or tilde operator. Now where you will find it? So if you look at your keyboard just below the escape key you have this special key.
Now if I say enter oh we got minus 13 that's weird. Why minus 13? So, let's do a research on this now. So, if we go back to my paint and let's do the calculation. So, how do you find this? Now, to understand this, first of all, we need to find what is complement means. Now, when you say complement, it will simply do reverse of your binary format example. If you say complement 1, it will give you 0. And if you say complement 0, it will give you 1.
1 so this is your complement of 12 but hold on what is this number and why we got minus 13 that's weird right Now if you look at minus 13, why minus 13 is this number? We have to find out that now. Now to understand this concept, again as a programmer, you need to know all this concept because when you do any course for IT or CS, this is what they will teach you in the first year.
We can store 12, we can store 13, we can store 15, 20, 21. or maybe 1000 but how to store negative numbers now to store negative numbers we first of all we don't store negative numbers in the system we always store positive numbers so even if you want to store this minus 13 you need to convert this number into positive number first and the way you can do that is with the help of two's complement now how do you find two's complement So to find 2's complement, we need to find 1's complement plus 1.
So when you say 2's complement, it is 1's complement plus 1. Now how do you find 1's complement? It's easy actually. First, convert this number into binary format. Now what is 13? It's 00001101. This is a binary format of plus 13. So this is 13, okay? We want to find minus 13, right? So you have to find 2's complement of this number. Now let's find the 1's complement. And how do you find 1's complement?
So you will get 1111 0010. So this is one's complement of 13, but we want to find two's complement, right? So what you will do, you will say plus one. Now this is so when you say zero plus one, it will give you one. And then we got one. Then we got 001111. now this number here this is minus 13 okay so this is 2's complement of 13 which is minus 13. we got it and now if you compare this number with complements of 12 they are same and that's why you got so when you say complements of 12 you got minus 13 right so that was the answer so that's how you can work with complements so when you want to store negative number this is what you do Now I would recommend you to try different numbers here. Maybe maybe compliments of 45 maybe compliments of 121. So try it out and let's let's see what answers you get there. So that's your compliments operator. The next one we have is bitwise and now if you remember in the previous video we have talked about logical operators right. So in logical, we have AND and OR, right? So here as well, we can use bitwise AND and bitwise OR. So in AND, remember this thing. So whenever you have AND, and if you have two conditions, and both are true, then only you will get true. And in OR, if you have at least one true, you will get true, if you remember. So if you remember this table, right, that's what you're going to do here now. So let's do bitwise AND. Now, which operator we use here? So we don't use AND because A-N-D-AND is for logical operations.
So let's take 12, ampersand 13, okay? So we got these two numbers, 12 and 13. If I say enter, you got 12. But why 12? So let's find out using bitwise. So let's go back to our paint, and here we'll convert 12 into binary format, which is... 00001100 this is 12 binary format right and then we have 13 binary format which is 00001101 and now we need to find out the bitwise here right so we have to work with bits so let's compare this bit with this bit Now if you remember AND operation, if both are 1.
So in this case, this is 0, 1, so it will be 0. This is 0. This is 1. This is 1 because both are 1s. And then we got 0, 0, 0, 0. And if you compare this output, this is 12, right? So you got bitwise AND. So this was an AND operation. And that's why you got 12. Let's do or. So we'll say 12 or. So now for or we have to use pipe.
0 0 0 0 1 1 double 0 that's 12 0 0 0 0 1 1 0 1 that is your 13 right and let's do or so when you say or if any 1 is 1 so your output is 1 right so we got 1 we got 0 1 1 0 0 0 0 and that's your 13 right so this is 13 let's verify and if i say enter oh we got 13 right so this works so this is how you work with bitwise and and bitwise or in fact you know you might be thinking only 12 and 13 you will get maybe some other number let's try so it's a 25 and maybe uh 31.
And you can see we got 24. Again, this is your homework, right? So try it on your notebook. So open your notebook and convert 25 into binary format. Convert 30 into binary format. And just do AND operation. And of course, you will get 24. The next one we have is XOR. Now in XOR what you do is it's actually very simple. When you say you have two numbers which is 0 and 0. Now since we don't have any 1 here we will get 0. Then you have 0 1. Now in this case you have odd number of 1 right. So which is 1 1. So you will get answer 1. Now when you say 1 and 0 so we again we have odd numbers of 1 which is 1 and when you have when you don't have odd numbers of 1 you can see we have two 1s which is even number so you will get 0 okay so when you have odd numbers of 1 you.
If you want to remember in a shortcut way it's very simple if both the numbers are different then you can go for 1 right so we have 0 1 is 1 1 0 is 1 so that's very simple this is how you perform XOR option this is XOR and you can represent that with the help of cap symbol. So let's do that here again we'll take two different numbers here we will go for let's go for 12 again the same number 12 cap 13 here Oh, I just made a mistake there. 12 cap 13. And you can see the answer is 1. But why 1?
We have done that. 0, 0, 0, 0, 1, 1, double 0. And then we have 13 here, which is 0, 0, 0, 0, 1, 1, 0, 1. And now let's do XOR. So if you have two different numbers than one, right? So they're different one. They are same zero, zero, zero. All are same number zero. So we'll go for zero, zero, zero. So the only answer you got here is one, right? So that's why we got one. That's how you perform XOR operation. So you can try here with different numbers, right? Again, the same number 25 and 30 tried out and let's see what you get. In fact, I will do it here. So I will say 25 cap 30 and you've got 7.
Let me know in the comment section what you get here. Now once you have done with AND, OR, and XOR, let's move towards left shift. I love this concept of left shift. So what is left shift here? So what I will do is, first of all, let me try something here. So I would say 10. Now when you say left shift, you have to use this left shift operators, which is left hand side.
Now when you say let's go to paint here and let's do calculation. Now when you say 10, what is 10? Now I will go for I will ignore the earlier zeros. Okay, so just because I am damn lazy to write every time that I will directly go with 1010. So again, we have we do have four zeros before but ignore them. So we got 1010 and then we have to apply a left shift operator here.
So when you say 65, again, we do have zeros there, but we ignore zeros because they are zeros. Here as well, when you say 1010, we do have a dot. So we do have a dot. And then after that, we have multiple zeros. In left shift, what we do is we shift the bits on left-hand side. Okay, I don't know for which side you are, so you are facing me, so for you, this side, right?
Okay, so now, so when you say left shift, you have to shift bits, but how many bits? You have to shift two bits, right? Example, if you see the output here, so you are shifting 10 by two bits. So when you said two bits you got 1010 and then you need to shift on left hand side two bits. So you will add two more zeros then we have a dot. So we were having a dot after zero but after shifting you have shifted two zeros on this side right. Now this number here is 40. Now how do you verify? Go with the normal calculation right. 2 raised to 0, 2 raised to 1, 2 raised to 2, 2 raised to 3. So this 1 is 2 raised to 3 and then we got 2 raised to 4 and then we got 2 raised to 5. So consider this one and consider this one. So this is 32 and this is 8. So 32 plus 8 is 40, right? So we got the answer. So that's how you find left shift. How about right shift?
And that's why we got two. So that's how you work with left shift and right shift. So that's your bitwise operator. So we have talked about complement. We have talked about and, or, x, or, and left shift and right shift. So again, when you do scientific calculation, this will be useful there. So I hope you're enjoying this. Let me know in the comment section. If you need some more improvement, let me know on which topic.
