ProgrammingModerateTCS NQT · Infosys

The binary-reflected Gray code of i is g(i) = i ^ (i >> 1). What is g(4)?

  1. A. 12
  2. B. 2
  3. C. 6
  4. D. 7

Correct answer: C. 6

4 is 100 and 4 >> 1 is 010, so the XOR is 110 = 6. 12 shifts left instead of right, 2 forgets the XOR, and 7 applies the Gray decoding (i ^ i>>1 ^ i>>2) instead of the encoding.

More C Programming questions

Exams that ask this topic