Data Structures & AlgorithmsModerateCodeforces Div2 B · bit tricks

Problem: What does the following print?

Problem: What does the following print?
print(bin(12 & -12), 12 & -12)
  1. A. 0b1100 12
  2. B. 0b11 3
  3. C. 0b1 1
  4. D. 0b100 4

Correct answer: D. 0b100 4

The expression x & -x isolates the lowest set bit, which for 12 = 0b1100 is 0b100 = 4. Thinking it returns 12 or the bit index are common mistakes.

More Coding Interview Problems questions

Exams that ask this topic