Consider the function int func(int num) { int count = 0; while(num) { count++;…
2017
Consider the function
int func(int num) {
int count = 0;
while(num) {
count++;
num >>= 1;
}
return(count) ;
}For func(435) the value returned is
- A.
9
- B.
8
- C.
0
- D.
10
Attempted by 368 students.
Show answer & explanation
Correct answer: A
The function func counts the number of bits in the input integer by repeatedly right-shifting until zero. Converting 435 to binary yields 110110011, which contains exactly 9 bits. Consequently, the while loop executes 9 times before num becomes zero, resulting in a return value of 9.