Course Content

  • 2.5 bitwise_operator

Course Content

JavaScript Bitwise

JAVA SCRIPT BASICS

Java script Bitwise.

(00:00)

Hello guys, welcome to our series Java script, in our last segment we covered arithmetic operators and comparison operators. In our today’s segment we will cover Bitwise operators and other operators. Let’s see Bitwise operator first, what is Bitwise operator? Basically Bitwise means, if you know binary language then you must know it is made up of 0 and 1.

So in this binary language we have some operations available, like and operations, or operations, not operations and xor operations etc., so all these operations are performed on binary digit. Let’s say I open my sublime text, you must know in binary 1 and 0, if I perform and operation on this. So if I take 0 and 0, then the answer which we get is 0, and means if both the values are same then the answer will be 0.

So here I will get 0, if I take 0 and 1 then I will get 0, among 0 and 1 answer is 0, if take 1 and 0 then it will be 0, if I take 1 and 1 then and then only my output will be 1. So this was and operation in our binary language. So this was our AND in binary, now let’s see OR in binary and its operation is performed.

As we saw in AND if both values are different then we get answer on basis of that, if it is 0 and 0then 0 if it is 0 and 1 then 0 is 0 and if it is 1 and 1 then 1. When do we get 1 as output? When both our input will be 1 then only our output will be 1. Now let’s see what is the case in OR?

In OR if value is 0 and 0 then 0, if value is 1 and 0 then my output will be 1, if value is 0 and 1 then output will be 1 and if the value is 1 and 1 then definitely the output will be 1. So here our binary operations are getting performed, if it is 0 1 then o if it is 0 0 then 0 if it is 1 1 then 1. So these were our binary operations.

 Same after AND and OR comes XOR and NOT, in we have different values then only we will have answer. For example, first we will see NOT, NOT in binary, in NOT ~ this symbol is used. ~of 0 our answer will be 1 and ~of 1 our answer will be 0. Means whatever our value is we will do its NOT, means changing the value. In binary we have only two values either 1 or 0, so not of 0 will be 1 and not of 1 will be 0.

Now remains XOR, so XOR in binary. In XOR what happens, we will copy these outputs, we saw AND and OR so in AND we get 1 as output when our both values are 1 and in OR we get 1 as output when one of the value is 1. But in XOR if both the values are same then it should be 0 if anyone of the value is 1 then output will be 1 but if both the values are 1 then answer will be 0. So this is the difference in XOR means exclusive or.

So here you can see there is a difference between OR and XOR , in last you can see where 1 and 1 comes it changes to 0. So in AND sign will be &, in OR sign will be |and sign of NOT is ~ and in case of XOR the sign will be ^. Okay so these were our Binary operations or Bitwise operations. It is called as Bitwise operations because it works on every bit, 0 and 1 are considered as bit. So 0 bit and 0 bit if we perform AND operation on it will be 0. If both values are different then 0 only in case of both values as 1 answer will be 1.

(05:22)

So we learned here in binary what is the output of Bitwise operations. So now we will perform a program, I have to copy from my last program, here html , Example Bitwise Operator, save Bitwise, script, in script I will add var x=5 , I will take another variable y= 9. Now I will perform some operations on x and y.

First is Bitwise AND, so here I will write document.write we can bitwise also or bitwise x and y, this is our header, Bitwise operator, okay so here we will write x and y this will be our Bitwise operation, save and reload our program and let’s see what will be our output. Okay, so here you can we in bitwise operation the output is 1, now we have to understand how does this happen, so in bitwise what happens actually?

 So here we have x=5 and y=9, here I came to our AND operation, what was the value of x? it was 5 so in bitwise operation how do we write 5? In binary 5 will be 0101, okay, in binary the first position is two to the power 0, second is two to the power 1 then one is two to the power 2 and I last comes two to the power 3. So in this way as our step increases power of two also increases.

So we know, two to the power 0 means 1, means 0 power of two is 1, next is two to the power 1 i.e. 2, so this is my position of 2 where 0 is written, here comes two to the power two i.e. 4 so you can 4 plus 1, I will write it down, x= 5, if written in binary, then x= (2^3) +(2^ 2)+ (2^1)+ (2^0), so in this direction means in left hand side direction it will keep on increasing like two to the power 4, two to the power 5etc.

So it will start from two to the power 0, then two to the power 1, then two to the power 2 then two to the power 3, so we know value of 0 is 1 and value of two to the power one is 2 and value of two to the power 2 is 4 and value of two to the power 3 is 8. So total if I write 0000 then this 1 will come at 0th position and this 2 will come at second 0th position 4 will come at this zero position ad 8 will come at this 0 position.

 So if want to indicate 0 in binary then I will replace this all by 0, at this position also 0, at this position also 0, here also 0 and here also 0. So all these powers of 2 i.e. 8 4 2 and 1 will be multiplied by 0, that’s why if I want x=0 it will become0. If I want 0 1 2 3 to be represented in binary numbers, so in that case x=0 will be four times 0 if I want to represent x=1 then I don’t need these, I will need only 1 so I will multiply these 4 positions with 0 and this position will be multiplied by 1.

So here will come 0001 , so this is my 1 in binary, now if I want 2 in binary then what will happen? 2 in binary means we don’t want this position, we don’t want this also, this we want and this we don’ want. Because we want 2, I will multiply this position by 1 and rest of the position will be multiplied by 0. So it will be 0010, i.e. 0 12 then comes x= 3, so 3 means 2 plus 1. So here also I have to multiply by 1 and here also by 1 so it will be 0011 equal to 3.

So in this way we will increase 1 and we will move forward. If we want 4, here we have 4, so 0100 equals to 4, okay then comes 5 so for 5 I need to add 1 in 4 so x= 0101 i.e. 5. Now if I want 6, 4 plus 2 is 6, so x= 0110 i.e. 6 then comes 7, for 7 4 plus 2 plus 1, so here 0111, i.e. 7, next is 8 as we already have 8 so x= 1000=8, then comes 9, which is 8 plus 1, so x= 1001= 9.

So this is our method for writing 0 to 9 in binary, so now in my program value of x is 5 and value of y is 9. So my values will 5 and 9, so 5 will be 0101 and 9 will be 1001, x= 0101 and y = 1001, now I have to perform bitwise AND operation between these two, so for that what will we do? For AND we saw when it is 0 0 then 0 if one value 1 then also 0 but when both values are 1 then 1.

So here you can see x&y, here you can see, if we start from here then 0 1i.e.0, 1 0 i.e. 0, 0 0 i.e. 0 and 1 1 i.e 1. Here our x is 5 and y is 9, but value of x AND y is 0001 i.e. 1. That’s why when we performed x AND y operation we got 1 as output. We need to have very good understanding when we work with bitwise operators, we need to understand the system language i.e. binary language then only we can understand what operation is getting performed with x and y.

So this was our bitwise x and y, I hope the explanation which I gave here, how binary numbers are generated bitwise, how AND and NOT x and y are generated, how we check in binary our bitwise operations. Our next operation is (14 seconds gap) bitwise OR operation, okay so our x and y are 5 and 9 respectively, so here with x and y I perform OR operation, so 0 1 i.e.1, 1 0 i.e. 1, 0 0 i.e. 0, and 1 1 i.e. 1, so this was our OR operation based on this condition. So I am getting answer as 1101 which means here 1 1 0 1, means 8 plus 4 plus 2 plus 1 so answer should 13, so I will save this and reload here, so I am getting 13 as value of x OR y.

Now I have to perform XOR operation, so here x XOR y operation, so here x XOR y, let’s check the value. In OR when we have same value then we have to take 0 in XOR, but if one value is different then we have to take 1. So here two out of 1 i.e. 1100, double one double zero, means 8 plus 4 is 12. So answer generating should be 12. Okay, so x XOR y is 12.

Now comes our NOT operation, NOT operation is not performed on two it is performed on only 1. So let’s say bitwise NOT of x, means value of x will be NOT, so what is the value of our X? It is 0101, currently 5, by replacing every 0 by 1 we will get NOT of x. so let’s change first 0 to 1, then 1 to 0 , then 0 to 1 then 1 to 0, so we are getting 1010, 1010 means 8+2 i.e. 10, so we should get 10 as answer, but here in bitwise we are getting -6 as answer. Getting -6 means the calculation which we considered here has some change.

Let’s see why did we get -6 here, so basically whenever we perform NOT of any number we do not positive number as answer, we will get the number generated in negative. If I am performing NOT of 5 then basically we should get 10, so NOT of 5 should be 10 according to our calculation, but that does not happen. NOT always generate minus number, so this our -3, currently we have x= 5, binary number of 5 is 00000, so if we write this in 16 bits, we will write this 14 times, (3 seconds gap) so after 14 we write 101, so this 5 according to hexadecimal position, but if take -3 as x then in -3 my every 0 will be changed to 1 if I change all the zeros into 1 then my number will be generated. 11111111111111010, so this is my -3. So this is -3 in binary, that’s why when we performed bitwise operation on 5 we did not get positive number, we got negative number.

Means we will always get negative number on performing NOT of any number. If we take one more example of this, let’s say bitwise instead of x I will take y, what is the value of y? it is 9, value of 9 is 1001 so at 9 we should get 0110, and 0110 we have 6, so if perform NOT of 9 then we should get 6 as per our calculations, but we will not get this, we will get some negative number, so we are getting -10, so we will not get answer in positive when we perform NOT. So here 10 we get by 8+2, so it is like x=1010, means the opposite of 0101 is getting generated in minus.

That’s why understanding bitwise operation is very difficult because it works on binary language. Now we will move onto our slide, we performed first four operations, AND, OR, XOR, and NOT. Now comes our left shift, so what happens left shift? Our values get shifted o left side as many times as we have mentioned. What is written in slide? Bitwise left shift means a<<b, so if value of a is 5 according to our program and value of b is 9, means x and y is 5 and 9.

So what will left shift do? It will generate a number, if I write this in this way (4 seconds gap) bitwise left shift x means two times our left shift sign and here left shift. So what will happen due to this? Value of x which is 5, in that 9 times left shift will be performed, and that many values will get added. So let’s say I take var z=2, for less calculation I am taking this number, here I will take z, here I will take z, save and reload, so here you can see after doing left shift z I am getting 20, means my 5 is getting changed to 20.

Logic behind this is, what happens in left shift? This operation will be performed, whatever number is given to x will be multiplied by 2 to the power of y, means in our case what is x? x is 5 so here 5 will come multiply inside bracket 2 to the power, now what is y in our case? Y is 2 so here 2 will come, 5*(2^2), two times power means 2 into 2 i.e. 4 and 4 into 5 will be 20.

If here instead of 2 I take 3, then here it will 2 to the power3 i.e. 2*2*2 i.e. 8, and 8*5 is 40. So here I will get 40 as answer, so x left shift whatever our number is multiply with our number, so what answer does the left shift operation gives us? Our answer in short is x into 2 to the power whatever the number is given.

So this was our left shift. Now in same manner we have right shift also, (7 seconds gap) basically what happens in left shift? The number which we tell that many zeros get on adding and my number gets shifted to the left side that many times.

For example 5 left shift 3 is getting performed, so here what will happen? Value of 5 is 0101 so in this I have to add three times zero as z= 3. I will shift my number to left side by adding zero three times. The number generated here which is the output i.e. 40. Earlier we used 2, our output was 40 so for 40 if it is 2 then 2 times , the number which is generated here is 40 i.e. 10 plus, here what is happening? 1 2 4 8 16, 16+4 i.e. 20, so this is my 20.

So in left shift the number or digits which we are mentioning will shift to left side that many times. If we are repeating same thing with right shift then our number shift to right side that many times. So here if I am performing my right shift, here I have taken 2 and here 5, so in 5 two times my right shift will be performed. So here my output I am getting is 1, so right shift of 5 is 1. If I have taken here 2 then whatever my digits are gets shifted to right hand side. So let’s say my 5 is 0101, so if I have to move it in right then every digit will shift to this position.

So my number, first 0 will be 0, then my 1 will be replaced by this 0,i.e. 0, this 1 will come at place of this 0, i.e. 1 and this 0 will come at place of this 0, i.e. 0, so here output generating is 2 i.e. 0010, but our output is 1. So this was first time and now I have to do this second time, because its value is 2 so same if perform right shift for second time then my 1 will be shifted here, i.e. three times 000 and 1 so 0001.

Means we have to place zeros at the empty places on left, initially my 1 shifted from here to here and now I will move this from here to here i.e. from 0 to 1. So my answer I am getting is 1. Now if you don’t want to perform shifting then same the formula which we applied above we can apply that here also in right shift. That is x divide by 2 to the power y, okay so my x is 5 and what was 2 to the power y it was 2so 2 into 2 i.e. 4 so if I divide 5 by 4 then my answer will be 1.

So in both ways I am getting answer 1.if you don’t want to calculate left shift and right shift in binary then these two formulas which we have given here x*(2^y) and x/(2^y) can be used to perform left and right shift, as both will give us same answers.

Now it is clear what does left shift do and what does right shift do? How the operations are performed in background? And how answers are generated? Last is bitwise right shift with zero. Now we have to perform right shift with zero.

For that, we will first add br tag, (8 seconds gap) here will come right shift (7 seconds gap) so here what is our output? 1. It is common thing when we performed right shift we filled the empty spaces on left with zeros. So right shift and right shift with zeros are same operations, in both the cases whatever empty space is there means, let’s come back to explanation, when we moved this on right hand side that time what we did, when 1 was shifted at place of 0 and when 0 was shifted at place of 1, then the first position i.e. 2 to the power 4 got empty, so we placed zero over there.

Again when we moved 1here and this zero was moved at place of 1 then at our fourth position which was getting empty there we again added 0. So this bitwise shift with zero, at the empty spaces which are created due to shifting there we add zeros. So this was our bitwise operations, so from our slide we have covered everything, AND, OR, NOT, XOR, bitwise left shift, bitwise right shift and bitwise right shift with zero, we performed everything and these are its outputs.

Guys try all these operations which we performed today and the techniques which we taught you that is how does it work? that you have to definitely check how it works in actual. So this was our bitwise operator, in our next segment also we are going to continue operators only in which we will cover logical and all assignment operators.

Thank you

(video duration- 29 minutes 58 seconds)

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere