ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
int x = 181, c = 0;
while (x) {
    x &= x - 1;
    c++;
}
printf("%d", c);
  1. A. 4
  2. B. 8
  3. C. 3
  4. D. 5

Correct answer: D. 5

x & (x - 1) clears the lowest set bit, so the loop runs once per 1-bit (Kernighan's bit count). 181 = 10110101 in binary has five 1s, so c = 5.

More C Programming questions

Exams that ask this topic