ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned n = 1000;
    int p = -1;
    while (n) { n >>= 1; p++; }
    printf("%d", p);
}
  1. A. 10
  2. B. 6
  3. C. 9
  4. D. 512

Correct answer: C. 9

p starts at -1, so after the 10 shifts that 1000 needs (its bit length) it holds 9 = floor(log2 1000). 10 forgets the -1 start, 6 counts the set bits of 1000, and 512 is the highest power of two rather than its exponent.

More C Programming questions

Exams that ask this topic