Data Structures & AlgorithmsModerateCodeforces Div2 B · bit operations

Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code.

Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code.
A, B = 29, 15
print(bin(A ^ B).count('1'))
What is printed?
  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

29 = 11101 and 15 = 01111, and their XOR 10010 has 2 set bits. Counting differing positions by subtraction gives the same answer in this example.

More Coding Interview Problems questions

Exams that ask this topic