Bird
Raised Fist0

Consider the following buggy implementation of sum of two integers without + operator. Which line contains the subtle bug that causes incorrect results or infinite recursion?

medium🐞 Bug Identification Q14 of Q15
Bit Manipulation - Sum of Two Integers Without + Operator
Consider the following buggy implementation of sum of two integers without + operator. Which line contains the subtle bug that causes incorrect results or infinite recursion?
ALine 3: base case condition if b == 0
BLine 6: sum_ calculation using XOR and MASK
CLine 8: recursive call with sum_ and carry
DLine 7: carry calculation missing left shift
Step-by-Step Solution
Solution:
  1. Step 1: Analyze carry calculation

    Carry must be (a & b) << 1 to shift carry bits to correct position.
  2. Step 2: Identify missing left shift causes infinite recursion

    Without shifting, carry never reduces to zero, causing infinite recursion or wrong sum.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Carry missing shift breaks carry propagation [OK]
Quick Trick: Carry must be shifted left by 1 bit [OK]
Common Mistakes:
MISTAKES
  • Forgetting carry shift
  • Incorrect base case
  • Not masking intermediate results
Trap Explanation:
PITFALL
  • Carry calculation looks correct but missing shift causes subtle infinite recursion or wrong sums.
Interviewer Note:
CONTEXT
  • Checks if candidate spots subtle bitwise operation bugs affecting correctness.
Master "Sum of Two Integers Without + Operator" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes