ProgrammingHardCompetitive coding (CP)

How many loop iterations run? Compute with the program.

How many loop iterations run? Compute with the program.
#include <stdio.h>
int main(void) {
    unsigned x = 0xF00D;
    int c = 0;
    while (x) {
        x >>= 3;
        c++;
    }
    printf("%d", c);
}
  1. A. 6
  2. B. 4
  3. C. 16
  4. D. 11

Correct answer: A. 6

Each x >>= 3 drops three bits; 0xF00D = 61453 needs 16 bits, so it takes ceil(16 / 3) = 6 shifts to reach 0. 4 treats each shift as removing a hex digit, 16 shifts one bit at a time, and 11 divides by 3 instead of by 8.

More C Programming questions

Exams that ask this topic