# Lecture 5: Bitwise Operators, For Loops, Operator Precedence & Variable Scoping

https://www.youtube.com/watch?v=yjdQHb2elqI

[00:03] Hello ji, how are you all?
[00:03] This is Love Babbar.
[00:05] And welcome to the channel Codehelp.
[00:07] This is the lecture number 5 of our DSA series.
[00:09] Today we are going to talk about the Operators.
[00:11] Specifically - Bitwise operators.
[00:15] We have already covered while loop, today we will see FOR loop.
[00:15] We have solved 18+ pattern questions in the previous lecture.
[00:24] Till now we haven't solved any question on some platform, but today we are going to solve easy and medium level questions on Leetcode.
[00:35] You will learn a lot in this lecture.
[00:35] So stay tuned.
[00:39] Let's start with operators.
[00:41] Bitwise operators.
[00:41] Very easy.
[00:47] First we will see these 4 things.
[00:49] AND, OR, NOT, XOR.
[00:55] These are there symbols.
[01:01] All these operators work on bit levels and not on numbers.
[01:17] We saw that this operator will return 1, if both the conditions on its side are true.
[01:23] If a=2, b=3 and I do a&b.
[01:35] This will give output like this.
[01:47] Let's see its truth table.
[02:01] AND gives 1 in output when both x and y are 1.
[02:12] So both bits should be 1 inorder to return 1 in output incase of AND.
[02:20] Let's take another example.
[02:22] Let's say a=5, b=7, we will get 101 in output.
[02:40] This is how & operator works.
[02:42] Now let's talk about OR operator.
[02:45] This video is sponsored by Crio.
[02:50] The world's leading project building platform.
[02:52] If we want a great job, then along with DSA and problem solving skills, we also need to have good projects with us.
[03:00] Then Crio is the place for you.
[03:01] You will get full stack program, backend program.
[03:05] The projects which you are going to make here are internship level projects.
[03:11] You are going to do everything practically.
[03:13] At the end of this program, you will have such projects which will boost up your resume.
[03:17] Also, they have fellowship program which guarantees your placement.
[03:24] All the programs comes with the free trial of 1 week.
[03:28] Also you will get sure scholarship.
[03:30] You can avail the maximum discount by clicking on the link given in the description.
[03:34] You can use this coupon code too for availing the maximum discount.
[03:39] And these coupons can be applied to any program.
[03:41] OR is represented by this symbol.
[03:44] This symbol is available on the left side of you ENTER key.
[03:48] Let's see its truth table.
[03:48] Incase of OR, Z is 1 if any of X and Y is 1.
[04:00] So this is its truth table.
[04:11] Let's take an example of a=2, b=4.
[04:15] This is the binary representation of these two.
[04:18] See, this is our output.
[04:23] Let's take another example.
[04:26] A=3, b=6 and ans = 7
[04:37] Now let's see NOT.
[04:43] For NOT we use tilt(~) symbol.
[04:47] It will simply invert the number- 0->1 and 1->0.
[04:52] See here, a = 2.
[05:03] But we know that integer is of 4 bytes, it means, it will have these leading zeroes too.
[05:14] Now on taking its NOT, it will invert all the bits.
[05:21] Now all these zeroes are converted to 1 and this 1 on the end will change to 0.
[05:28] Now what will be our output if we will print this?
[05:36] As we can see that first bit is 1, it means a negative number will get printed.
[05:43] Then to find this number we have to take its 2's compliment.
[05:43] For that, first we will take 1's compliment and then simply add 1 to it.
[05:58] So we got our answer as -3.
[06:12] Got it.
[06:16] We have already seen this conversion in the previous lectures.
[06:23] For finding 2's compliment, first we have to find 1's compliment and then add 1 to it.
[06:28] Let's discuss the last XOR operator.
[06:34] Here is its truth table.
[06:42] XOR will give 1 if only one of X and Y is 1.
[06:48] It will give 0 if both the numbers are 0 or 1.
[06:56] So this was our XOR operator.
[07:06] Let's take one example.
[07:08] For a=2, b=4, our answer is 6.
[07:20] For a=5, b=7, answer is 2.
[07:34] Now we understood XOR too.
[07:37] Bhaiya implement it once, then we will understand it more.
[07:40] Okay we have to implement these 4 things.
[07:46] Let's take int a=4 and b=6.
[07:58] First I am printing a&b.
[08:16] I will print all others also in the same way.
[08:20] A|B.
[08:26] ~A.
[08:32] A^B.
[08:43] Now let's run it.
[08:45] A&B is giving us 4, let's see how.
[08:50] A is 4 and b is 6.
[08:55] See we are getting 4.
[08:55] So & gave us correct output.
[09:06] Now let's do OR.
[09:06] Again we got correct answer 6 for OR too.
[09:15] Now let's find NOT of A.
[09:22] 4 is 000.....00100 in binary.
[09:29] Now on taking it's not, it will become 111111..........111011.
[09:35] First bit is 1, it means it is a negative number.
[09:41] Then we will find its 1's compliment by inverting every bit.
[09:54] Now we just have to add 1.
[10:00] So we got our answer as -5.
[10:07] See, we got -5 in the output too.
[10:10] Now let's see for XOR.
[10:17] See we got 2 which we calculated.
[10:29] Now we will talk about left shift operator and right shift operator.
[10:44] This is how we write left shift operator.
[10:53] It mean that shift 5 towards left by 1.
[10:59] This is 5 in binary.
[11:04] Now for left shift, we have to move every bit by 1 towards left.
[11:09] So this will be our output.
[11:16] Let's take the example of 3<<2.
[11:29] This time we have to shift every bit by 2.
[11:34] We got our answer as 12.
[11:45] We can see that whenever we are left shifting any value, we are getting n*2 as our answer.
[11:45] Will we get this all the time?
[11:54] No, but we get this answer in majority cases, but not in every case.
[12:00] Let's see why.
[12:23] Now let's say we have a number which has this bit as set bit.
[12:31] Now this is it's left shift by 1.
[12:36] This number now has become a negative number.
[12:42] So whenever we will left shift a large number, then that number will become negative.
[12:45] So you should keep this thing in mind.
[12:50] For smaller numbers we will get our answer as 2*n.
[12:54] But in some cases it create negative numbers.
[12:56] This is your left shift operator.
[12:56] Now let's talk about the right shift operator.
[13:04] If I say 151, in this we have to shift every bit towards right by 1.
[13:13] So this will be our answer in case of 52.
[13:30] It means if I am right shifting any number by 1, then I am getting n/2 in answer.
[13:41] If I am right shifting any number by 2, then I am getting n/2*2 in answer.
[13:47] So we have understood a major logic of left and right shift operators.
[13:52] One thing to keep in mind is, whenever we are left shifting or right shifting any number, padding is always with 0.
[14:07] Padding means, when I am shifting this number towards right, then these two number digits will be 0.
[14:13] Same applies for left shift.
[14:13] So padding is always done with 0 in case of positive number.
[14:25] But when we take negative numbers.
[14:29] Then it is opposite.
[14:32] In case of negative numbers, padding depends on compiler, we don't know what it will do.
[14:42] It can do padding with 0 or 1.
[14:48] Now let's see some examples of these operators.
[15:16] Now let's try these and check whether we are getting correct output or not.
[15:21] Printing all these cases.
[15:50] We got 8, 4, 38 and 76.
[15:56] Sorry sorry, we made a mistake in calculation part.
[16:08] No no, we wrote 19 there, instead of 21.
[16:08] Let's correct it and check.
[16:23] See we got 84.
[16:25] In short, everything is working fine.
[16:28] We have understood both left shift and right shift operator.
[16:30] We have also seen that exception of large numbers.
[16:37] Let's simplify one thing for you.
[16:37] In the while loop we were writing this i=i+1 inside the loop.
[16:53] Can we write it in some other way?
[16:53] Yes, for that I am going to tell you about increment and decrement operator.
[17:02] I can write this i=i+1 as i++.
[17:10] I can write it as ++i too.
[17:16] I can do something like this also.
[17:24] This one is known as post increment.
[17:30] This one is pre increment.
[17:35] Same applies for decrement case.
[17:44] This one is post decrement.
[17:46] And this one is pre decrement.
[17:51] I can write it i-=1.
[17:56] If I have write sum = sum+b, then I can write it like this too sum+=b.
[18:05] This one was your utility.
[18:07] Now let's see them in depth.
[18:12] Bhaiya what does post increment mean?
[18:20] i++.
[18:22] It means that first use the value and then increment it by 1.
[18:31] Let's see with an example.
[18:33] If i=4.
[18:39] And I write it as int a = i++.
[18:44] Now let's check what are the values of 'a' and 'i'.
[18:51] Post increment means, increment after using, so it means 'a' will be having the old value of i which is 4 in this case.
[19:01] And 'i' is incremented after this operation, it means i is 5 now.
[19:07] Got?
[19:08] Let's understand it once again.
[19:10] Let's say int i=3.
[19:14] Sum = a + i++.
[19:20] And a is 2 here.
[19:26] We know that first we will perform the calculations, then we will increment.
[19:36] So sum will be 2+3(old value of i) = 5
[19:40] And after this i will become 4.
[19:43] First use the old value and after that increment it.
[19:49] You will get MCQ questions from this part.
[19:56] Now let's talk about pre increment operator.
[19:58] It says, first increment me, and then use it.
[20:08] Let's say int i=11.
[20:14] And if we will do a=++i.
[20:17] Then it means, first increment then use me.
[20:22] So in this case, first i will change to 12 and then 12 will be assigned to A also.
[20:31] Let's see the old example.
[20:36] a=2, i=3, sum= a+(++i).
[20:41] In this case, first we have to increment i, then only we can use it in our calculation.
[20:46] So first i will become 4 and then sum will be 6.
[20:50] Ok then.
[20:53] This was your pre increment and post increment.
[20:57] Same things are application on pre decrement and post decrement.
[20:59] I-- means first use me, and then decrement me.
[21:12] In this case first it will get used in calculation and then decremented to 2.
[21:20] Same applies for --i.
[21:23] Int a = --i, means first it will decrement and then assign it to a.
[21:30] So both 'a' and 'i' will be equal to 2.
[21:37] So now in the loop, we can simply write i++ inplace of i = i+1.
[21:48] So now we are familiar with pre increment and post increment
[21:57] Now let's try to do experiments with this
[22:03] Let's print all these values
[22:44] In this case, first we have to increment and then print, so in first case our answer will be 8
[22:59] Now i =8
[23:01] In this case, first we have to print and then increment, so 8 will be printed and then i will change to 9
[23:12] Now here, First it will use the value and then decrement it
[23:19] It will print 9 and then change its value to 8
[23:23] In this last case, it will decrement it to 7 first and then print
[23:33] So this should b our answer = 8, 8, 9, 7
[23:38] See we got correct output
[23:43] So you should also play with these operators because they are very easy and are asked in MCQs
[23:56] Now let's move ahead
[23:58] I will give you 5 questions and you have to tell the output in the comment section
[24:05] Those who will answer correctly, top 3 students will get the gifts
[24:09] Now let's move ahead
[24:25] So you must seen the 5 questions
[24:29] Now let's move on to the next topic which is FOR loop
[24:32] We have studied while loop, now let's see FOR loop. It will work in the same manner, but just the syntax is different
[24:45] Let's try to print the counting from 1 to N
[24:53] First let's take N in input
[24:57] Now it is looking more good
[25:12] Now I have to print the count from 1 to N
[25:22] In the FOR loop, first define the iterating variable, then write the condition and then change it's value
[25:53] Here we are printing value of i
[26:00] So this is our code to print the counting
[26:13] Now let's see how it is working
[26:19] FOR loop consists of 3 things
[26:25] This is the initialization part, any variable which you have declared are initialized here
[26:35] 2nd thing is condition ( it is similar to the condition in the while loop)
[26:45] 3rd one is update, where I can increment the value of decrement it or divide/multiply
[26:54] Are all these conditions necessary? No. You can simply write this also for(; ; )
[27:24] First let's run this
[27:37] First let me remove this increment and run it. See it is working
[27:46] I can remove this condition from here and write it inside the loop something like this
[28:16] See it is working fine
[28:20] What is this. Here this loop does not know when it has to stop, so it print infinite numbers
[28:44] Can I stop it somehow?
[28:53] We have something called 'break', which says break the loop
[29:06] Break - will take you out of the current loop
[29:16] Currently we are in this FOR loop, this break will help us in exiting from this loop
[29:27] let's run and see
[29:30] See this time it printed numbers only till 5
[29:37] It worked for numbers from 1 to 5, but when i was 6, this loop breaks
[29:57] This is how break statement works
[29:59] Now let's see what else we can do with this FOR loop
[30:08] Can I write 2 variables? Yes I can write
[30:18] Can I add 2 conditions? Yes I can write
[30:31] Similarly, I can do 2 updations
[30:39] Let's write this printing statement and run it
[30:52] oooo, why it is behaving like this
[31:01] Why it printed so many negative values
[31:04] See we have written this condition wrong, because this condition will always be true and this loop will keep on executing
[31:17] Let me correct it
[31:23] Now let's run it again.
[31:30] See now it is working fine
[31:33] So 1 thing is clear that we can write multiple initializations or conditions or updations
[31:45] You can add as many as conditions or updations as you want
[32:07] Oh, we have to put comman and not '.'
[32:13] See it is working fine
[32:17] So now you have understood this also
[32:24] Now let's understand the flow of the FOR loop
[32:37] You come here only once, then you come to this condition. If condition is true, then you come inside the body and execute all statements inside it
[32:53] Once it is done, you come to this update part, and then you can come back to conditions, and you keep on moving in this loop until this conditions holds true
[33:09] This is how your FOR loop gets executed
[33:11] Let's do one question
[33:16] Let's say, we want to find the sum of all numbers from 1 to N
[33:20] First we will do these simple question and then move on to the tricky one
[33:23] First we took N in input, and then initialized sum with 0
[33:29] Now we will write a For loop with these initialization, condition and updations
[33:44] So it is understood that i will start from 1 and go till N
[33:49] And inside this loop, we have to write, sum = sum+i , or we can write it like this too, sum+=i
[34:02] and outside this loop, we will simply print our answer
[34:05] You can use any loop from FOR loop or WHILE loop
[34:13] See it is giving correct output
[34:22] Let's do one more question
[34:32] You have understood how to use break statement
[34:35] We have something called Fibonacci series, you can pronounce it whatever you feel comfortable with
[34:47] It look something like this
[34:58] Every number here is the sum of previous two numbers
[35:03] So this is how this series is made
[35:16] So any number f(n) is made from f(n-1) + f(n-2)
[35:25] Got it?. Now let's print it
[35:35] Let's say I want to print first 10 numbers
[35:41] N = 10
[35:53] Now let's define first two numbers, a = 0 and b=1
[36:02] So we know first two numbers and with the help of them, we can find all the other numbers
[36:08] Now we will write a For loop with these initialization, condition and updations. This loop will run for 10 times
[36:31] Now first we will find the next number by adding a and b
[36:55] Then we will print this new number
[37:02] So now we got sum = 1 by adding 0 and 1
[37:23] Now we will put B's value in A and Sum's value in B
[37:34] Here order matters a lot
[37:36] Let's say sum is 2, then if you will transfer 2 into b first, then you will not be able to transfer B 's value to A, because B's old value is replaced with the new one
[38:04] So first we will transfer B's value to A and then sum's value to B
[38:11] So first we will write a= b, and then b = nextNumber
[38:22] See we are done
[38:26] Now let's run it
[38:28] See we got the correct output
[38:36] Bhaiya, where are the starting 2 characters?
[38:37] We haven't printed them. Let's do it now
[38:49] Now you will get these 2 also
[38:52] See, we have this series
[38:58] First number is A, 2nd is B, and these numbers will help to generate the next number
[39:05] Now once we have found a new number, we have to shift this A and B ahead
[39:12] So for that, first we will put B in A and then Next number's value in B
[39:27] This is how this loop is working
[39:34] If you haven't understand it clearly, no need to worry. We will see some more questions ahead
[39:42] Let's take one question on prime numbers
[39:54] To find that this n is prime or not, we use to divide it by all numbers from 1 to N, if it gets divided by any number, then it is not a prime number, else it is a prime number
[40:39] In short, if we get n%i == 0 (where i is any number between 1 and N), then it is not a prime number
[41:06] Now let's try to implement it using FOR loop
[41:08] First I took N in input
[41:12] Then we started a loop from 2 to less than N,
[41:29] Inside this loop, we will check, if(n%i == 0)
[41:43] if we got 0, then it means, it is not a prime number
[41:56] So we will print that it is not a prime number
[42:02] If it gets divided by one number, do I need to divide it with any other number? No
[42:23] So I want to stop this loop, what will I use? Break, yes
[42:28] So we wrote 'break' here
[42:30] But if don't divide then we have to check for other numbers, because it may give 0 with them
[43:11] If it did not get divide by 2, then it does not mean that it is a prime number, we have to check for all number from 2 to n-1
[43:26] So in short, we don't have to write this else condition. Let me remove it
[43:37] Can I make a variable isPrime which I initialized with 1
[43:47] Whenever I get any number which divides N, I will make isPrime 0
[43:55] and when i will come out of this loop, then I will check if IsPrime is 0, then it is not a prime number
[44:20] Else it is a prime number
[44:29] Now let's run it
[44:35] 14 is not a prime number. Good
[44:39] 7 is a prime number
[44:41] 101 is also a prime number
[44:47] 112 is not a prime number. See it is working completely fine
[44:52] Here I wanted to tell you the use of break
[44:58] Let's take the case of 14
[45:03] here we can see that 2 is diving 14, do I even need to go ahead and check for other numbers? No, we came to know that it is a not a prime number
[45:16] So we will use break and end the program here only
[45:26] You have understood break. Now lets see 'Continue'
[45:39] Continue is use to skip a particular iteration. Now what is this iteration
[45:45] When flow of program comes inside the loop for first time, we call it as first iteration, when it comes for 2nd time, it is called as 2nd iteration and so on
[46:19] Now let's see continue. It is used to skip any interation
[46:22] For eg- Let's take this FOR loop, where I am simply printing "Hi", "hey"
[46:57] continue, "Reply tou karde"
[47:05] So this is my code
[47:08] For i=0, it will print Hi and hey
[47:16] But when it will come to continue, it will end this iteration here only
[47:21] and it will keep on doing so as many times as time loop is getting executed
[47:33] Inshort, the code which is written below continue is unreachable now
[47:40] Got?
[47:50] Let's code this part too
[47:55] First we will write a For loop with these initialization, condition and updations
[48:12] Writing all those statements
[48:44] Let's run it
[48:47] Hi hey, hi hey, hi hey
[48:54] It didn't executed this "reply tou karde" statement
[48:59] Got it
[49:01] We are familiar with both break and continue
[49:08] Now you have to do these output questions and write there answers in comment section
[49:20] These are those questions
[49:39] Now let's come to the variables and their scope. We know variables but what is this scope?
[49:50] We are going to talk about the life time of any variable and can where all we can use it
[50:02] If I will write print 'a' here, then I am getting this error
[50:20] Compiler is saying that what is this 'a'
[50:28] So to use any variable, we have to declare it first
[50:32] Here I declared this a
[50:35] I didn't initialized it with any value, but still it gave me 1 in output
[50:41] Here our compiler has given a garbage value to it
[50:48] This time I gave it value of 3
[50:52] So we came to know that first we have to create a variable, inorder to use it
[51:05] So we have understood this thing
[51:11] Now let's say I have this 'if' condition here, can I access this A here?
[51:38] Let's see
[51:40] It is working, it means that if I have declared any variable outside the if condition, then I can use it inside the IF condition too
[51:49] Now let's say, I declared a variable here, int a = 5
[51:57] Will we get error? No
[52:02] This A is declared inside this scope, so it will not give error
[52:18] now If i try to print A here
[52:23] See I got 3 and 5
[52:29] So this A can not be accessed outside this IF block, because it is declared inside it
[52:41] This time, a=3 will be printed, because a=5 is only inside the IF block
[52:54] Let's say this variable's name is B and I try to access it, it will error
[53:17] This B will end once this IF block is executed
[53:25] So I can's use any variable outside the block in which it is declared
[53:42] Got it, right
[53:55] But If I will redeclare B twice in the same block, then I will get this error
[54:16] Now let's try it out FOR loop
[54:48] Let's say, I declare 'i' outside this loop
[54:56] Then this loop will use this 'i' which it has declared explicitly
[55:05] You can see we got our output 8 times
[55:14] But why this 'i' was not used
[55:18] Now see, if I will remove this 'i', then it will look for 'i' outside this block and use it
[55:31] Got it
[55:36] So if any variable is defined inside a block, then we can't access it outside that block
[55:43] I can define the variables with same name inside different blocks, but we can't define multiple variables with same name in same block
[56:13] Will we get any error? Let's check
[56:20] See it worked fine
[56:26] I can define the variables with same name inside different blocks, but we can't define multiple variables with same name in same block
[58:04] Now let's talk about operator precedence
[58:15] Inshort, which operator has more authority or which operator will be executed first when there will be multiple operators
[58:20] Let's take this example
[58:34] If I will divide first (3/4 =0), then I multiply and then add, like this we will get 5
[58:56] But if we would have multiplied first and then divided and added, then we would have got `6
[59:04] So which operator to use first. This is why we are studying operator precedence
[59:17] Do we need to mug up this? No
[59:23] We can simply use brackets to avoid this confusion
[59:38] We don't need to learn the table, we can simply use brackets
[59:49] Let's once see the table
[59:53] This is operator precedence table
[59:57] Precedency increases from bottom to top and left to right
[01:00:09] Bhaiya, I didn't get this. No need to learn it. Everyone uses brackets
[01:00:26] Till now we are done with Bitwise operators, AND, OR, XOR, NOT, Left shift, right shift
[01:00:44] For loop, components of For loop, flow of FOR loop, modifications in FOR loop, variable scoping, operator precedence, use of brackets
[01:01:04] Now let's do questions on all these topics
[01:01:13] Let's do this question. It is an easy question
[01:01:23] You have a number and you have to find the difference between the product of its digits and the sum of its digits
[01:01:50] Bhaiya how we will find these individual digits from this N? I will tell
[01:02:05] N = 234
[01:02:08] if I will do n%10, I will get 4
[01:02:14] So we got our last digit, but we have to find the rest of the digits too
[01:02:21] So after that I will divide n by 10, so I will be left with 23
[01:02:29] n%10 will give me its last digit and n/10 will omit last digit from the original number. By doing these steps again and again, I can find all the digits of any number
[01:03:13] We have to do these steps until the N is zero
[01:03:26] You can see that we are doing same thing again and again, so we will use a loop here
[01:03:32] Let's say we have 456
[01:03:35] To find 6, we will do 456%10. After this we will divide it by 10 to get 45 and again find it's modules to get 5. Now again by doing these steps I will get 4.
[01:04:15] Let's understand the code now
[01:04:16] Once we get the digit, we will find its product and sum and then repeat these steps
[01:04:47] I will initialize prod with 1 and sum = 0
[01:04:53] Prod = Prod*digit, sum = sum+digit
[01:05:03] At the end, we just have to return prod - sum
[01:05:16] Now let's implement
[01:05:17] We have to run the loop until the N is 0
[01:05:30] in rem =n%10
[01:05:35] I initialized two things, prob =1, sum =0
[01:05:45] prod = prod*digit
[01:05:59] sum = sum + digit
[01:06:06] Now I have to divide this N, n = n/10
[01:06:14] so our answer is prod - sum
[01:06:22] At the end we will return our answer. We will understand this return when we will do functions.
[01:06:38] Whenever you are solving question on any platform, first run it on your own test cases
[01:06:49] See, we got our output
[01:06:59] Now I will submit it. It will run this solution on multiple test cases and the tell me whether my code is correct or not
[01:07:07] Accepted
[01:07:10] See. Faster than the 100% of CPP users. You have to solve it and put this screenshot on LinkedIn😁
[01:07:31] So we have learnt how to find the digits of any number. So we are done with our 1st question
[01:07:43] Now let's see this 2nd question
[01:07:51] We are given an unsigned integer, and we have to find how many set bits it have
[01:07:57] For eg, in binary 5 is represented as 101, so it has 2 set bits
[01:08:04] In 8 (1000), number of set bits are 1
[01:08:11] This is what you have to print
[01:08:21] Let's see, what they have given us in input
[01:08:27] So, they have given us N. Let us print it once to check in what form they have given us the data, in binary or integer
[01:08:37] Okay, so they have given us the number in integer form
[01:08:50] So we have to tell how many set bits it have
[01:08:58] First solution - Convert it to binary and then count
[01:09:13] We will check the last digit and then right shift these numbers by 1. We will keep on doing this for 32 times, because integer is of 32 bits
[01:09:50] We have to right shift it for 32 times or till the number is not 0
[01:10:08] while(n!=0)
[01:10:14] checking last bit
[01:10:24] We will declare a variable count=0
[01:10:29] To check the last bit, we will do , if(n&1)
[01:10:38] If last bit is 1, you will get true, on doing its & with 1, else it is false
[01:10:53] We will increase the count if we come to know that last bit is 1
[01:10:58] and after that we will right shift n by 1
[01:11:02] And at the end, we will return our ans
[01:11:13] See it is working fine
[01:11:16] Let's run it for this test case. It is giving correct output
[01:11:33] We are just checking the last bit, if it is 1 then we will increment count and right shift the number by 1
[01:11:47] Let's say our number is this
[01:11:50] Here last bit is 0, so we will simply right shift it by 1
[01:11:55] Now this is our new number
[01:12:01] Now we will again check last bit. This time it is 1, so we will increment the count and right shift the number
[01:12:11] Again we will check last bit. This time it is 1, so we will increment the count and right shift the number
[01:12:21] Now this whole number has become 0, so this loop will break
[01:12:24] Let's submit
[01:12:27] Accepted
[01:12:30] Your code was faster than 100% of the C++ user.
[01:12:49] In this video, we talked about operators, For loop, precedence, variable scope, flow of FOR loop, shifting in operators, bit wise operators
[01:13:07] We also solved 2 questions where we beat 100% solutions by our run time efficiency of our code
[01:13:20] This video's length is already more than 1 hour, so we will do other questions in the next video
[01:13:30] But if you want to do those questions, then I will list down them here, you can solve them
[01:13:38] First question is reverse integer (leetcode)
[01:13:47] 2nd question is Compliment of base 10 integer (leetcode)
[01:14:00] 3rd question is number compliment(leetcode)
[01:14:07] After this we have to do a question, where we will convert binary to Decimal
[01:14:13] For eg - 1010->10, or 0010 to 2
[01:14:24] Also, Decimal to Binary Eg - 11 to 1011
[01:14:31] After that we will do, sqrt(x) where X is an integer
[01:14:40] We just have to print our answer in integer and no need to find the decimal part
[01:15:00] One question more, Find power of a, b
[01:15:09] I will explain these questions in the next video, but if you want to do beforehand, then you can try them
[01:15:17] So in this video, we covered so many concepts. If you enjoyed it, then write it in comment section
[01:15:25] Answers to all the MCQ questions should be written in the comment section
[01:15:30] So we will meet in the next video where we will solve these questions.
[01:15:33] Video length is too much these days, so I need to think something about it
[01:15:40] Will see you in the next one, till then, Bye
