Question 86
What is the output value of 'count' after executing the following C code?
#include <stdio.h>
int main(void) {
int n = 29;
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
printf("%d", count);
return 0;
}Detailed Solution
Kernighan's algorithm clears one set bit in each iteration. 29 in binary is 11101_2, which has four 1-bits. The loop runs 4 times, so count = 4.