GATE Computer ScienceHardGATE CS (PYQ pattern)

Let g(x) = x XOR (x >> 1) be the 8-bit Gray code of x. For how many x in 0..254 is g(x) equal to x + 1?

  1. A. 1
  2. B. 0
  3. C. 2
  4. D. 8

Correct answer: A. 1

If x ends in exactly k ones, x + 1 = x XOR (2^(k+1) − 1). So g(x) = x + 1 requires x >> 1 = 2^(k+1) − 1, i.e. x = 2^(k+2) − 2 or 2^(k+2) − 1. The first ends in 0 ones, forcing k = 0 and x = 2 (g(2) = 3). The second ends in k + 2 ones, contradicting k. Only x = 2 works.

More Digital Logic questions

Exams that ask this topic