GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For how many of the 256 eight-bit patterns x does the 8-bit two's complement negation −x (= x' + 1, carry out discarded) contain exactly as many 1 bits as x?

  1. A. 29
  2. B. 30
  3. C. 16
  4. D. 35

Correct answer: B. 30

Write x = y·1·0^k (lowest 1 at bit k, y the 7 − k bits above it). Then −x = y'·1·0^k, so popcount(x) = p + 1 and popcount(−x) = (7 − k − p) + 1, where p = popcount(y). Equal iff p = (7 − k)/2, so k is odd: k = 1: C(6,3) = 20; k = 3: C(4,2) = 6; k = 5: C(2,1) = 2; k = 7: 1 (x = 10000000). That is 29, plus x = 0 (−0 = 0): 30.

More Digital Logic questions

Exams that ask this topic