ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint32_t m = 0x00A4u;
    for (int i = 0; i < 32; i++)
        if (m >> i & 1u) printf("%d ", i);
}
  1. A. 3 6 8
  2. B. 2 5 7
  3. C. 2 5 6
  4. D. 1 4 6

Correct answer: B. 2 5 7

0xA4 is 1010 0100, so bits 2, 5 and 7 are set, and the loop prints those positions. The loop tests bit i with (m >> i) & 1, not m >> (i & 1).

More C Programming questions

Exams that ask this topic