ProgrammingVery hardGATE CS · Campus placements

Adding two non-negative ints a and b without +, a loop repeats a = a ^ b and b = (old a & old b) << 1 until b is 0. Starting with a = 5 and b = 3, what does a finish as?

  1. A. 8
  2. B. 6
  3. C. 2
  4. D. 9

Correct answer: A. 8

The steps are (5,3) to (6,2), then (4,4), then (0,8), then (8,0). The carry chain ends with the sum 8.

More C Programming questions

Exams that ask this topic