Data Structures & AlgorithmsModerateCodeforces Div2 B · bit tricks

Problem: How many trailing zero bits does 40 have in binary? Run the code.

Problem: How many trailing zero bits does 40 have in binary? Run the code.
print((40 & -40).bit_length() - 1)
What is printed?
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: A. 3

40 = 101000 in binary, so the lowest set bit is at position 3. The expression 40 & -40 isolates it.

More Coding Interview Problems questions

Exams that ask this topic