Coding · stretch-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Sum of Two Integers
mediumbit-manipulation
Problem
Compute a + b for two integers without using the + or - operators anywhere in your code. Only bitwise operations, shifts, comparisons, and assignments are allowed.
- Both operands are small, well within a couple thousand in magnitude.
- Assume the answer fits in a signed 32-bit integer. Your code must handle negative operands and negative results correctly.
Examples
a = 4, b = 9→13.a = 6, b = 5→11.6 ^ 5 = 3is the sum without carries and(6 & 5) << 1 = 8is the carry;3 + 8 = 11, and recursing on(3, 8)gives11 ^ 0 = 11.a = -1, b = 1→0. In 32-bit two's complement,-1is all ones; the carry propagates out of the top bit and is discarded, leaving 0.
Hints
- Add two bits by hand: the sum bit is
x XOR y, the carry bit isx AND y. How do you combine these across all positions? a ^ bis the sum ignoring carries;(a & b) << 1is the carry to add on. Repeat until there is no carry.- In Python, ints are unbounded, so a negative carry shifts left forever. Mask every intermediate to 32 bits and convert the final pattern back to a signed value.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.