Starting by incrementing from count(2) Count(2): 110110011__ itr0 Count(3): 001101100__itr1 Count(4): 000011011__itr2 Count(5): 000000110__itr3 Count(6): 000000001__itr4 Count(7): 000000000__itr5 After count(7) we get out of the loop.
I have a little bit different approach. As it is a right shift that also we have to do 2 times per iteration so as discussed in previous lectures in right shift we divide no. By 2^(no. Of bits to right shift) so here no. Of bits to shift is 2 so we will divide by 2^2 i.e. by 4. So when we count the iteration along with dividing no. The loop will terminated at 5th iteration as at 5th iteration the value i.e. the no. Will be 0 after dividing. So the final answer is 5+2 = 7. i.e. count =7
let me explain u we take the value the give num and divide it by 2*2^n (first 2 is no. of swifting we are doing ,then net 2 is poissible output that is 0and1 power x which is iteration) smaller then 1 because we need to make the binary number zero which is less then 1@@debjyotisarkar9578
Here num is 435 (110110011) and count=2, if we shift right the number like 001101100(3) ,here 3 after count (2 )give that, for understanding Similarly, 000011011(4) 000000110(5) 000000001(6) 000000000(7) After counting it gives 7 if we count after 2 and another simple way is 2+5=7.
When count = 7 num contains 0 value.So the condition became false and we came outside of the loop and return the value of count which is 7.So 7 is the right ans.
Ans is 7. #include int fun(int a){ int count=2; while(a){ count++; a>>=2; } return count; } int main(){ int a; a=435; fun(a); printf("the value of a: %d ",fun(a)); return 0; }
Quick question on the first task, how is the count = 9 instead of 10, when it's being incremented before the shift operation? while(num) { count++; num>>= 2; }
The answer to the homework is 7. But how to remember the bit representation of such a large number like 435. Do the question contains the bitwise representation of the given number?
The answer is 8 because after integer to binary conversation we will get 11 digits number, So in 5 iterations it will remove 10 digits from no. Then 1 digit will remain for that it will iterate one more time then count could be 6 since count start from 2 so, the answer will 8.
Ans in 7 sir bcz 435 in binary 110110011 and given that shifting by 2 when count is 2 the while loop will terminate after 5th iteration (the value of num is then num =000000000) and the Value of count returns is 5+2=7 ans..
As regarding condition of while loop we take them as true or false and all numbers other than zero are considered as true and 0 is considered as false...
Instead of doing it BIT by BIT, We can just use this this rule, We know that when we right shift a number by any number, Say 435>>1 = 217, which we can calculate by 435/2^1, By doing it again and again in calculator until it reaches 0, we can calculate the number of iterations to make that value to 0, we can find the answer I guess ?. Correct me If I am wrong peace ✌️.
We hitted the like button....and subscribed also ......but ur not solving anyone's doubt......please solve ours doubt...I request u sir.. why we use =operator in num>>=1...
Anything is wrong in my question.if you won't reply me then what is the need of doing sessions .... it's not cleared for me .... please help me sir🙏🙏🙏🙏.... Why we use=operator in num>>=1.