GATE | GATE-CS-2014-(Set-2) | Question 20
Consider the function func shown below:
int func( int num)
{
int count = 0;
while (num)
{
count++;
num >>= 1;
}
return (count);
}
|
The value returned by func(435)is __________.
(A) 8
(B) 9
(C) 10
(D) 11
Answer: (B)
Explanation: The function mainly returns position of Most significant bit in binary representation of n. The MSD in binary representation of 435 is 9th bit.
Another explanation:
>> in right shift. In other words, it means divide by 2.
If keep on dividing by 2, we get: 435, 217, 108, 54, 27, 13, 6, 3, 1.
Therefore, the count is 9.
Quiz of this Question
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...