Bird
Raised Fist0

Given the following Python code implementing sum of two integers without using + operator, what is the output when calling getSum(1, 2)?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Sum of Two Integers Without + Operator
Given the following Python code implementing sum of two integers without using + operator, what is the output when calling getSum(1, 2)?
A2
B3
C1
D0
Step-by-Step Solution
Solution:
  1. Step 1: First call getSum(1, 2)

    sum_ = (1 ^ 2) & MASK = 3, carry = ((1 & 2) << 1) & MASK = 0
  2. Step 2: Since carry is 0, return sum_ = 3

    Base case reached, final sum is 3.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    1 + 2 = 3 matches output [OK]
Quick Trick: XOR sum with carry zero returns final sum [OK]
Common Mistakes:
MISTAKES
  • Confusing carry calculation
  • Off-by-one in recursion
  • Returning carry instead of sum
Trap Explanation:
PITFALL
  • Some think carry is added immediately or confuse XOR and AND roles, leading to wrong sums.
Interviewer Note:
CONTEXT
  • Checks candidate's ability to trace bitwise recursion and base case.
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