Sum of Two Integers
Add two integers without using plus or minus.
Why does this pattern fit?
Restate the exact job
Add two integers without using plus or minus.
XOR adds without carry; AND shifted left computes the carry.
O(word size) time · O(1) space
Language integer width and signed overflow rules must be explicit.
How to solve Sum of Two Integers
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Add two integers without using plus or minus.
Why Bit manipulation fits
XOR adds without carry; AND shifted left computes the carry.
State to maintain
Partial sum and carry bit masks.
Transition
Repeat sum=a XOR b and carry=(a AND b)<<1 until carry is zero.
Time and space
O(word size) time · O(1) space
Counterexample to the tempting mistake
Language integer width and signed overflow rules must be explicit.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.