ProgrammingHardTCS Digital · Infosys InfyTQ

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 1000, c = 0
while (n != 0)
    n = n & (n - 1)
    c = c + 1
end while
Print c
(& is bitwise AND.)
  1. A. 5
  2. B. 7
  3. C. 6
  4. D. 10

Correct answer: C. 6

Each step clears the lowest set bit, so c is the number of 1 bits in 1000 = 1111101000: 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic