ProgrammingVery hardProduct company OA

What is printed?

What is printed?
for (unsigned n = 5; n < 8; n++)
    printf("%u ", n ^ (n >> 1));
unsigned g = 13, b = 0;
for (; g; g >>= 1)
    b ^= g;
printf("%u", b);
  1. A. 7 5 4 11
  2. B. 7 6 5 9
  3. C. 4 5 7 9
  4. D. 7 5 4 9

Correct answer: D. 7 5 4 9

n ^ (n >> 1) is the binary-to-Gray conversion: 5 = 101 → 101 ^ 010 = 111 (7); 6 = 110 → 110 ^ 011 = 101 (5); 7 = 111 → 111 ^ 011 = 100 (4). The second loop is the inverse (Gray-to-binary): b = 1101 ^ 0110 ^ 0011 ^ 0001 = 1001 = 9. Check: 9 ^ (9 >> 1) = 1001 ^ 0100 = 1101 = 13.

More C Programming questions

Exams that ask this topic