ProgrammingModerateCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint32_t x = ~0u >> 3;
    int c = 0;
    for (; x; x &= x - 1) c++;
    printf("%d", c);
}
  1. A. 28
  2. B. 30
  3. C. 32
  4. D. 29

Correct answer: D. 29

~0u is 32 ones, and shifting right by three leaves 29 ones in the low bits. The loop counts exactly those set bits.

More C Programming questions

Exams that ask this topic