GATE Computer ScienceModerateGATE CS (PYQ pattern)

unsigned x = 0x369; /* binary 11 0110 1001 */ unsigned s = 0, c = 0; while (x) { unsigned t = x & -x; c++; s += t * c; x -= t; } What is s after the loop?

  1. A. 4721
  2. B. 4209
  3. C. 4821
  4. D. 873

Correct answer: A. 4721

The lowest set bits peeled off in turn are [1, 8, 32, 64, 256, 512], multiplied by 1, 2, 3, ...: sum = 4721.

More Programming and Data Structures (GATE) questions

Exams that ask this topic