Podcast Lookup

_14 Python Tutorial for Beginners _ Python BitWise Operators

Python To You

  • October 16, 2026
  • 13 min
Episode page

About this episode

No description.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

_14 Python Tutorial for Beginners _ Python BitWise Operators

00:00—
  1. Speaker 1

    Welcome back Aliens, my name is Ravind Reddy and let's continue with this series on Python.

  2. Speaker 2

    So till this point we have worked with operators, right?

  3. Speaker 1

    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.

  4. Speaker 2

    Called as tilde.

  5. Speaker 1

    So let's use it and that's your tilde sign and we'll give any number here where we'll go for 12. Now when you say enter what you are expecting come on go for a random guess here. Maybe minus 12 or maybe minus.

  6. Speaker 2

    1 something like that right.

  7. Speaker 1

    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.

  8. Speaker 2

    That's what it's doing. Now the same thing is applied on 12, right? And what is 12?

  9. Speaker 1

    12 is actually, if you convert 12 into binary format, you will get this. So you will get four zeros, double one, double zero. That's your 12 binary format. And I love this mobile.

  10. Speaker 2

    Phone model, right? Nokia double one, double zero. Everyone love it.

  11. Speaker 1

    Now we need to find a complement of it right so you have to find the complement of it so what you will do is you will reverse the number so this this 0 becomes 1 and then this is 1 then this is 0.

  12. Speaker 2

    0 1 1 1.

  13. Speaker 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.

  14. Speaker 2

    But if you don't know this concept, that's okay. You can learn here now.

  15. Speaker 1

    Now we have a concept of 2s complement because what happens is in your system, we can store.

  16. Speaker 2

    Positive numbers, right?

  17. Speaker 1

    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.

  18. Speaker 2

    This is the formula, okay?

  19. Speaker 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?

  20. Speaker 2

    It's easy now. We know how to find complement, right? So we have to reverse this number.

  21. Speaker 1

    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.

  22. Speaker 2

    So here we have to use ampersand symbol.

  23. Speaker 1

    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.

  24. Speaker 2

    Then only 1, right?

  25. Speaker 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.

  26. Speaker 2

    Okay.

  27. Speaker 1

    So for and we have ampersand. For or we have pipe. And then okay where you will find pipe. So it is above your enter button. So we'll say 12 or 13.

  28. Speaker 2

    Come on guess. Now in fact before entering here let's do calculation. Let's compare here.

  29. Speaker 1

    So let's go back here and let's do some calculation.

  30. Speaker 2

    So it will be.

  31. Speaker 3

    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.

  32. Speaker 2

    Or maybe 30. Yeah, 30.

  33. Speaker 1

    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.

  34. Speaker 2

    Will get 1.

  35. Speaker 1

    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?

  36. Speaker 2

    Let's verify. So let's go back here and do calculations. So what is 12?

  37. Speaker 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.

  38. Speaker 2

    But how 7? That's your homework, right?

  39. Speaker 1

    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.

  40. Speaker 2

    And then you will say 2. Okay, what's your guess? What is the output? I don't know. Let's try. So you got 40. But why 40? Let's find out.

  41. Speaker 1

    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.

  42. Speaker 2

    Now, how do we use it?

  43. Speaker 1

    Now in left shift what happens is imagine there's a box here. So imagine there's a this is a box and then we have a dot here dot symbol and then we have some zeros. Now of course right every number is represented this way. Example when you say five it's actually 5.0.

  44. Speaker 2

    In fact, we can have multiple zeros here, right?

  45. Speaker 1

    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?

  46. Speaker 2

    So left-hand side. So let's do that.

  47. Speaker 1

    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?

  48. Speaker 2

    It's very simple. So we also have a concept of right shift, right?

  49. Speaker 1

    What I will do is I will say I will go with the same number 10 and right shift with two bits. Now in left shift, if you are gaining bits in right shift, you are losing bits.

  50. Speaker 2

    Let's verify. So if I say enter, you can see we got two. But why two? Let's do it. Let's do it here.

  51. Speaker 1

    So we have one zero one zero s ten. So we have a we have a dot here. So in right shift, you shift on this side, right?

  52. Speaker 2

    Right inside. So you will be losing how many bits? Two bits, right?

  53. Speaker 1

    So you'll be losing this two bits. So what is remaining is one zero, which is this is remaining, right? And what is one zero?

  54. Speaker 2

    It's two, right?

  55. Speaker 1

    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.

  56. Speaker 2

    So that's it, everyone. Bye-bye.