ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode? (^ is XOR, & is AND)

What will be the output of the following pseudocode? (^ is XOR, & is AND)
Integer a = 10, b = 20, x, flips = 0
x = a ^ b
while (x > 0)
    x = x & (x - 1)
    flips = flips + 1
end while
Print a ^ b, flips
  1. A. 30 2
  2. B. 10 4
  3. C. 30 4
  4. D. 30 5

Correct answer: C. 30 4

10 = 01010 and 20 = 10100 differ in four places: a ^ b = 11110 = 30, and clearing its 1-bits one by one takes 4 steps. 4 bit flips turn 10 into 20: 30 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic