PROBLEM 75 OF 75

Sum of Two Integers

Add two integers without using plus or minus.

PATTERNBit manipulationWrite the bit truth table before the code.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Add two integers without using plus or minus.

WHY THIS FITS

XOR adds without carry; AND shifted left computes the carry.

COMPLEXITY

O(word size) time · O(1) space

COMMON FAILURE

Language integer width and signed overflow rules must be explicit.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗