#15 Python Tutorial for Beginners | Python BitWise Operators

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan 20 and let's give you this series on Python so till this point we have worked with operators right we have talked about assignment operator I panic operators and logical operators now it's time to focus on some hold operators and this time we are going to work with bitwise now if remember in one of the video we have talked about money formats right so this is where you will use them so far should the names suggest it is something you do with money formats right so what am I going to do now is with bits so let's get started so it's open Python and let's do this calculation now the first one so if you talk about bitwise we have six different operators we have complement operator then we have and all XOR left shift and right shift again let's talk the first one we just bombed them interpreter and if you look at this symbol of it it's weird right if we don't use this symbol match so this is called as tilde operator all till the operator now when you will find it so if you look at your keyboard just below the Escape key you have this special key call s tilde so let's use it and that's your tilde sign and will give any number here we will go for 12 now when you say enter what you are expecting come on Google for line of guess here maybe March 12 or maybe minus one something like that right 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 ripples of your bunny from an example if you say complement one it will give you zero and if you say complement zero it will give you one that's what he's doing now the same thing is applied on 12 right now and what is 12 12 is actually if you convert two into body format you will get this so you will get four zeros double one double zero that's your 12 and a format and I love this mobile phone model right no Caleb I wonder 1 0 everyone loved it 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 0 1 1 1 one 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 30 in Y minus 13 is this number that you have to find out that now now to understand this concept again as a programmer you need to know all this concept because even balance so when you do any course in for I T or CS this is what they will teach you in the first year but if you have if you don't know this concept that's okay you can learn here now now we have a concept of two's complement because what happens is in your system we can store positive numbers right we can store 12 we can store 13 we can store 15 20 21 or maybe 1000 but how to store negative numbers not the store negative numbers we first of all pick to be don't store negative numbers in the system we always tell 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 two's complement we need to find ones complement plus one this is the formula okay so when is a two's complement it is ones complement plus one now how do you find ones complement it's easy actually first convert this number into one a format now it is studied it's 12 0 0 0 0 1 1 0 1 this is a bunny format of plus 13 so this is studied ok we want to find - and in writes I have to find two's complement of this number now let's find the - complement and how do you find ones complement it's easy now we know how to find complement right so we have to reverse this number so you don't get 1 1 1 1 0 0 1 0 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 1 now this is so when you say 0 plus 1 it will give you 1 and then we got 1 then regards 0 0 1 1 1 1 now this number here this is minus 13 okay so this is two's complement of 13 which is mastered in we got it and now if you compare this number with compliments of 12 they are same and that's why you got so when you say complements oft well you got mastered in right so that was the answer so that's how you can work with complement so when you want to store a number this to do now I would recommend you to try different numbers here maybe maybe compliments or 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 alright so here as well we can use bitwise and and bitwise all so in and remember this thing so anyway you have and and if you have two conditions one and both are true then only you will get true and in odd 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 any because a and D and is for logical operations so here we have to use ampersand symbol so let's take 12 am person 13 okay so we got this 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 will convert 12 into panel format which is 0 0 0 0 1 1 double Z this is 12 binary format right and then we have produced by this format which is 0 0 0 0 1 1 0 1 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 then only 1 right so this in this case this is 0 1 so it will be 0 this is 0 this is 1 this is 1 because both of once and then we got 0 0 0 0 and if you compare this output this is 12 right so you got B twice and so this was and and operation and that's why you got 12 let's do all so it's a 12 all so now for all we have to use pipe ok so forth and we have am person for all we have pipe and then okay where you will find pipes I twist B it is above your Enter button so it is a 12 or 13 come on yes now in fact before entering here let's do calculations let's compare here so let's go back here and let's do a compare let's do some calculation so it will be 0 0 0 0 1 1 w that's 1200 0 0 1 1 0 1 that is your 30 in void and let's do all so when you say all if any one is one so so your output is 1 byte so we got one 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 quite ready in right so this works so this is how you work with bitwise and and bitwise or internal you know you might be thinking or the 12 and 10 you will get maybe some other number let's try so it's a 25 and maybe 31 or maybe 30 yeah 30 and you can see we got 24 again this is your homework right so try it on your notebook so put your notebook and convoy 25 do binary format converted into binary format and just do and operation and of course you will get only for the next one we have is X all now in X or watch it I switch this it's actually very simple when you say you have two numbers which is 0 and 0 now since we don't have any one 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 2 ones which is even number so you will get 0 ok so when you have odd numbers of one you will get one if form do remember in a shortcut way it's very simple if both the numbers are different then you can go for one right so we have 0 1 is 1 1 0 is 1 so that's very simple this is how you perform X order patient this is X or and you can represent that with the help of CAP simple so let's do that here how can we take two different numbers here we will go for let's go for 12 again the same number 12 cap 13 here oh that just made a mistake there 12 cap 13 and you can see the answer is 1 but why can't let's verify so let's go back to the go back here and do calculations so what is 12 we have done that 0 0 0 0 1 1 double zero and then we have put in here which is 0 0 0 0 1 1 0 1 and now let's do X or so if you have two different numbers than 1 right so they're different one they are same 0 0 0 all are same 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 Teddy dries out and let's see what what you get in fact I will do it here so I will say X 25 cap 30 and you've got seven but how seven that's your homework right I'll it mean 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 else I would say then now when you say left shift you have to use this left shift operators which is another left-hand side and then you will say - 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 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 fat here zeros okay so just because I am damn lazy to write every time that I would hardly go with one zero one zero so again we have we do have force it was before but ignore them so we got one zero one zero and then we have to apply a left shift operator here now how do we use it now in some shape - what happens is imagine there is 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 numbers represent this for example when you say 5 it's actually five point zero in fact we can have multiple zero zero right so you said 65 it's a again we do have zeros there but we ignore zeros because there are zeros here as well when you say one zero one zero 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 get enough for which side you are so so you are facing me so for you this side right so left hand side so let's do that okay so now so when you said left shift you have to shift bits but how many bits you need to shift two bits right example if you see the output here so you have shifting ten by two bits so when you say two bits you got 1 0 1 0 and then you need to shift on left hand side 2 bits so you you will add two more zeros then we have a dot so we were having a dot after 0 after shifting you have shifted two zeros on this side right now this number here is 40 now how do you verify go with a normal calculation right 2002's 2 1 2 is to 2 is to 3 so this 1 is to 3 and then we got to H to 4 and then we go out to a so fine so consider this one and consider this one so this is 32 and this is 8 so try to place 8 is 40 right so we got the answer so that's how you find left shift how about right shape it's very simple so we also have a positive right shift right what I will do is I will say over the same number 10 and right shift with 2 bits now in left shift if you are gaining bits in right shape you're losing bits that's what you find so if I say enter you can say we got to but why do let's do it again let's do it here so we have 1 0 1 0 s then so we have it we have a dot here so in right shift you shift on this side right right-hand side so you will be losing how many 2 bits right so you'll be losing these 2 bits so what is remaining is 1 0 which is this is remaining right and what is 1 0 H 2 right and that's why we got 2 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 all X or and left shift and right shift so again even when you do scientific calculation this will be useful there so I hope you are enjoying this is written in the comment section if you if you need some more improvement let me know on which topic so that's it everyone you
Info
Channel: Telusko
Views: 721,443
Rating: 4.9240208 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, program, operator, bitwise, example, complement, tilde, negative, 2's complement, 1's complement, conversion, AND, or, xor, find, left shift, right shift
Id: PyfKCvHALj8
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Sat Jul 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.