ProgrammingModerateTCS NQT · Infosys

What is the sum of popcount(i) for i = 1 to 8, where popcount counts the set bits?

  1. A. 12
  2. B. 13
  3. C. 21
  4. D. 8

Correct answer: B. 13

The popcounts of 1..8 are 1,1,2,1,2,2,3,1, totalling 13; a quick check is that 1..7 hold 12 bits (each of 3 positions set 4 times) plus one for 8. 12 stops at 7, 21 counts binary digits rather than set bits, and 8 counts the zero bits.

More C Programming questions

Exams that ask this topic