ProgrammingVery hardGATE CS · Campus placements

How many integers in the range 0 to 15 have an even number of 1 bits in binary?

  1. A. 11
  2. B. 8
  3. C. 7
  4. D. 2

Correct answer: B. 8

By popcount, 0..15 split as 1, 4, 6, 4, 1, so even popcounts give 1 + 6 + 1 = 8 (exactly half, as for any full range 0..2^k - 1). 7 forgets that 0 has zero (even) set bits, 2 forgets the six values with two bits, and 11 counts values with at least two bits.

More C Programming questions

Exams that ask this topic