11 / 75
M

Sum of Two Integers

#11
bit-manipulationmath

Add two integers using bitwise operations without using + or - operators.

Example:

Input:a = 1, b = 2
Output:3

Common Mistakes:

  • Not handling negative numbers correctly
  • Infinite loop with negative carry in some languages
  • Forgetting to shift carry left by 1

Notes:

Key insight: XOR gives sum without carry, AND gives carry positions. In Java, works with negative numbers due to two's complement.

11/75
Sum of Two Integers