GATE Computer ScienceModerateGATE CS (PYQ pattern)

The 8-bit binary-reflected Gray code of x is g(x) = x XOR (x >> 1). For how many of the 256 values of x does g(x) contain exactly two 1 bits?

  1. A. 56
  2. B. 28
  3. C. 21
  4. D. 8

Correct answer: B. 28

Bit i of g(x) is 1 exactly where bits i and i+1 of x differ (with a 0 assumed above the MSB). So the weight of g(x) is the number of 0/1 boundaries when x is read with a leading 0. Exactly two boundaries means x is one solid block of 1s that does not reach bit 0: choose the 2 boundary positions out of 8, C(8,2) = 28.

More Digital Logic questions

Exams that ask this topic