ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned n = 37;
    printf("%u", n ^ (n >> 1));
}
  1. A. 54
  2. B. 18
  3. C. 55
  4. D. 37

Correct answer: C. 55

The Gray code of 37 is 37 XOR 18, which is 100101 XOR 010010 = 110111 = 55. Shifting left instead of right gives a different value.

More C Programming questions

Exams that ask this topic