ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
x, count = 0b1011011, 0
while x:
    x &= x - 1
    count += 1
print(count, bin(0b1011011).count('1'))
  1. A. 4 5
  2. B. 5 4
  3. C. 5 5
  4. D. 6 5

Correct answer: C. 5 5

x & (x - 1) clears the lowest set bit each time, so the loop runs once per set bit. 0b1011011 has five ones, so both values are 5.

More Python Programming questions

Exams that ask this topic