ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
def pc(x):
    c = 0
    while x:
        x &= x - 1
        c += 1
    return c
print(pc(0b1011011), pc(255), pc(0))
  1. A. 5 8 1
  2. B. 4 8 0
  3. C. 5 7 0
  4. D. 5 8 0

Correct answer: D. 5 8 0

x & (x - 1) clears the lowest set bit each time, so the loop runs once per set bit. 1011011 has five ones, 255 has eight, and 0 has none.

More Python Programming questions

Exams that ask this topic