XOR and Arithmetic
What is this?
XOR is a light switch with a memory quirk โ flip it with the same value twice and it returns to where it started, so pairs cancel out and only the odd one left over survives. Building on that, you can even rebuild grade-school addition and division using nothing but switch operations. These problems lean on that self-cancelling trick and switch-by-switch math.
๐ก Fun fact: The self-cancelling nature of XOR powers the classic in-place swap of two variables without a temporary, and it is the same property that lets RAID disk arrays rebuild a lost drive from the others.
๐ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: XOR cancels equal pairs to zero, so XOR-ing matched values leaves the odd one out. Push that idea further and you can rebuild arithmetic itself โ addition becomes XOR for the sum plus a shifted
ANDfor the carry, and division becomes repeated shift-and-subtract.
The pattern
XOR's defining property is a ^ a == 0 and a ^ 0 == a. XOR a stream of numbers where everything appears in pairs and the pairs annihilate, leaving only the unpaired survivor. The same logic pairs indices against values: XOR all indices 0..n together with all array entries, and every matched index/value cancels, exposing the missing one.
The arithmetic problems strip operators down to gates. Addition without +: a ^ b gives the sum ignoring carries, (a & b) << 1 gives the carries, and you repeat until the carry is zero. Division without /: subtract the largest left-shifted multiple of the divisor you can, accumulating the corresponding power of two into the quotient โ long division in base two. Reasoning about how few bit operations reach a target leans on the same shift-and-count instinct.
The problems
- Missing Number โ XOR every index
0..nwith every value; matched pairs cancel and the gap remains. - Sum of Two Integers โ add without
+: XOR for the partial sum, shiftedANDfor the carry, loop until no carry. - Divide Two Integers โ shift-subtract long division; peel off the biggest shifted divisor and add its power of two to the quotient.
- Minimum Operations to Make the Integer Zero โ reason from set-bit counts about how many shift/subtract steps are required.
Key takeaways
a ^ a == 0makes XOR a self-canceling accumulator โ perfect for isolating an unpaired element.- XOR indices against values to surface a missing number in linear time and constant space.
- Addition decomposes into XOR (sum) plus shifted AND (carry), iterated until the carry clears.
- Division is shift-and-subtract: remove the largest shifted divisor and record its power of two.
- Watch sign and overflow boundaries when rebuilding arithmetic on fixed-width signed integers.
Core idea: XOR a number with itself and you get
0. So if you XOR every index0..ntogether with every value in the array, each present number meets its own index and cancels โ the lone survivor is the missing number.
Problem, rephrased
You're auditing a coat-check counter at the end of the night. The tickets were numbered 0, 1, 2, ..., n โ exactly n + 1 tickets handed out. At closing, n coats come back, each still pinned to a ticket, and every returned ticket is distinct. One ticket never came back. Which number is missing?
Formally (LeetCode 268): given an array of n distinct numbers drawn from the range 0 to n inclusive โ that's n + 1 possible values, with the array holding n of them โ return the single value that's absent.
| Input array | Range it's drawn from | Missing |
|---|---|---|
[3, 0, 1] |
0..3 |
2 |
[0, 1] |
0..2 |
2 |
[1] |
0..1 |
0 |
[9,6,4,2,3,5,7,0,1] |
0..9 |
8 |
Note the asymmetry that makes this work: the array has length n, but the universe of possible values has size n + 1. There's exactly one slot with nobody in it.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson