GATE | GATE-CS-2014-(Set-2) | Question 20

Consider the function func shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

int func(int num)
{
    int count = 0;
    while (num)
    {
        count++;
        num >>= 1;
    }
    return (count);
}

chevron_right


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

My Personal Notes arrow_drop_up