ProgrammingVery hardCompetitive coding (CP)

What is printed? The program inverts the Gray code 6 by XOR-folding its bits.

What is printed? The program inverts the Gray code 6 by XOR-folding its bits.
#include <stdio.h>
int main(void) {
    unsigned g = 6, n = 0;
    for (; g; g >>= 1) n ^= g;
    printf("%u", n);
}
  1. A. 4
  2. B. 6
  3. C. 5
  4. D. 2

Correct answer: A. 4

The loop XORs 6, 3 and 1, giving 6 ^ 3 ^ 1 = 4. That is the binary value whose Gray code is 6.

More C Programming questions

Exams that ask this topic