Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A Gray code of 4 bits is generated as g = n XOR (n >> 1) for n = 0 to 15. What is the Gray code value of n = 11?

  1. A. 29
  2. B. 5
  3. C. 14
  4. D. 13

Correct answer: C. 14

11 = 1011 and 11 >> 1 = 0101, so g = 1011 XOR 0101 = 1110 = 14. Shifting left instead (11 XOR 22) gives 29, reporting n >> 1 alone gives 5, and decoding 11 as if it were already a Gray code gives 13.

More Coding Interview Problems questions

Exams that ask this topic