GATE Computer ScienceHardGATE CSE · TCS

int count(unsigned x) { int c = 0; while (x) { x &= (x - 1); c++; } return c; } What is the value of count(0xB5D)?

  1. A. 7
  2. B. 9
  3. C. 10
  4. D. 8

Correct answer: D. 8

Each iteration clears the lowest set bit, so the result is the number of 1-bits in 0xB5D = 1011 0101 1101, which is 8.

More Programming and Data Structures (GATE) questions

Exams that ask this topic