GATE Computer ScienceHardGATE CSE

int cnt = 0; for (int i = 0; i < 100; i++) for (int x = i; x; x = x & (x - 1)) cnt++; What is the final value of cnt?

  1. A. 336
  2. B. 316
  3. C. 304
  4. D. 600

Correct answer: B. 316

The inner loop runs popcount(i) times, so cnt = Σ popcount(i) for i = 0..99 = 316.

More Programming and Data Structures (GATE) questions

Exams that ask this topic