Bird
Raised Fist0

Consider the following iterative bitwise sum function to add two integers without using the '+' operator. What will be the output of bitwiseSum(3, 5)?

easy🧾 Trace Q3 of Q15
Bit Manipulation - Sum of Two Integers Without + Operator
Consider the following iterative bitwise sum function to add two integers without using the '+' operator. What will be the output of bitwiseSum(3, 5)?
A5
B15
C7
D8
Step-by-Step Solution
Solution:
  1. Step 1: Initialize variables

    Set a = 3, b = 5.
  2. Step 2: Calculate sum without carry

    Use XOR: 3 ^ 5 = 6.
  3. Step 3: Calculate carry

    Use AND and shift: (3 & 5) << 1 = (1) << 1 = 2.
  4. Step 4: Repeat until carry is zero

    Next iteration: a = 6, b = 2. XOR: 6 ^ 2 = 4, carry: (6 & 2) << 1 = 4. Next: a = 4, b = 4. XOR: 4 ^ 4 = 0, carry: (4 & 4) << 1 = 8. Next: a = 0, b = 8. XOR: 0 ^ 8 = 8, carry: 0. Loop ends.
  5. Final Answer:

    Option D -> Option D
  6. Quick Check:

    3 + 5 = 8 [OK]
Quick Trick: Use XOR for sum and AND+shift for carry [OK]
Common Mistakes:
MISTAKES
  • Confusing XOR with OR for sum calculation
  • Not shifting the carry bits correctly
  • Stopping before carry becomes zero
Trap Explanation:
PITFALL
  • Choosing OR instead of XOR looks plausible but yields wrong sums.
Interviewer Note:
CONTEXT
  • Tests understanding of iterative bitwise addition logic.
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