Data Structures & AlgorithmsModerateCodeforces Div2 A · bit operations

Problem: What does the following print?

Problem: What does the following print?
print(bin(10).count('1'), (10).bit_length())
  1. A. 1 4
  2. B. 2 3
  3. C. 2 4
  4. D. 3 4

Correct answer: C. 2 4

10 is 1010 in binary, which has two set bits and bit length 4. Confusing bit_length with the highest set bit index (3) is the trap.

More Coding Interview Problems questions

Exams that ask this topic