GATE 2014 CS (CS2) – Question 20
For the C function below, find func(435).
int func(int num) {
int count = 0;
while (num) {
count++;
num >>= 1;
}
return count;
}
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 9 to 9
Explanation
Each loop increments count and shifts the positive integer right one bit. Since2⁸≤435<2⁹, its binary representation has9significant bits. After9shifts it becomes zero, so **9** is returned. This counts bit length, not set bits.