Bird
Raised Fist0

What is the output of getSum(-2, 3) using the recursive bitwise sum function below?

medium🧾 Code Trace Q4 of Q15
Bit Manipulation - Sum of Two Integers Without + Operator
What is the output of getSum(-2, 3) using the recursive bitwise sum function below?
A-5
B1
C0
D-1
Step-by-Step Solution
Solution:
  1. Step 1: Convert inputs to 32-bit mask

    -2 in 32-bit unsigned is 0xFFFFFFFE, 3 is 0x00000003.
  2. Step 2: Trace recursive calls

    sum_ = (0xFFFFFFFE XOR 3) & MASK = 0xFFFFFFFD, carry = ((0xFFFFFFFE & 3) << 1) & MASK = 4; next call getSum(0xFFFFFFFD, 4)
  3. Step 3: Continue until carry is 0

    Eventually returns 1, which is the sum of -2 + 3.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    -2 + 3 = 1 matches output [OK]
Quick Trick: Masking handles negative numbers correctly [OK]
Common Mistakes:
MISTAKES
  • Ignoring masking leads to wrong negative results
Trap Explanation:
PITFALL
  • Candidates often forget to mask or handle sign bits, causing wrong output.
Interviewer Note:
CONTEXT
  • Tests handling of negative numbers and masking in bitwise sum
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