GATE Computer ScienceHardGATE CS (PYQ pattern)

Over all 256 eight-bit patterns x (with −x and x − 1 computed modulo 2⁸), for how many is x & (−x) equal to x ^ (x − 1), where & is bitwise AND and ^ is bitwise XOR?

  1. A. 128
  2. B. 129
  3. C. 9
  4. D. 127

Correct answer: A. 128

For x ≠ 0 with lowest set bit L = 2^k: x & (−x) = L, while x ^ (x − 1) flips bit k and every bit below it, giving 2L − 1. These are equal only when L = 1, i.e. x is odd: 128 values. For x = 0: 0 & 0 = 0 but 0 ^ 0xFF = 0xFF, so it fails. Answer 128.

More Digital Logic questions

Exams that ask this topic