Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

What does this program print?

What does this program print?
x = 22
cnt = 0
while x:
    x &= x - 1
    cnt += 1
print(cnt, bin(22).count('1'))
  1. A. 5 3
  2. B. 3 2
  3. C. 3 3
  4. D. 2 3

Correct answer: C. 3 3

The statement x &= x - 1 clears the lowest set bit each time, so the loop runs once per set bit. The first value equals the popcount of 22, which is 3, and the program prints it next to the built-in count.

More Coding Interview Problems questions

Exams that ask this topic