ProgrammingModerateGATE CS · TCS NQT

What is printed?

What is printed?
#include <stdio.h>
unsigned g(unsigned n) { return n ^ (n >> 1); }
int main(void) {
    printf("%u %u", g(10), g(7));
}
  1. A. 13 4
  2. B. 15 5
  3. C. 15 4
  4. D. 14 4

Correct answer: C. 15 4

The Gray code of n is n XOR (n >> 1). For 1010 this is 1111 = 15, and for 0111 it is 0100 = 4.

More C Programming questions

Exams that ask this topic