◀ THE FOLD0ROOT.AI // WORLD II · RESPAWN · HARD RESET◆ .dlw.fold
THE FOLD / RESPAWN / HARD RESET / THE XOR SWAP

THE XOR SWAP

no temporary, and one input it destroys
1 WHAT IT IS · WHAT IT DOES · FACT OR FICTION
Three exclusive-ors swap two values with no temporary variable. It is exact — and it has one input it silently destroys. Point both names at the same storage and the value becomes zero, because the identity that makes the trick work is the identity that breaks it.

LIT verified live and exhaustively. Over all 65,536 pairs of 8-bit values in distinct slots the swap is correct 65,536 times. Aliased to itself, every value tested — 0, 1, 5, 127, 255 — comes out 0, and the original is unrecoverable. It saves one register and buys a precondition.
2 HOW IT WAS WEAVED · AI + HUMAN
The XOR swap is old assembly-language folklore, from an era when a spare register was worth a precondition. It survives mostly as an interview question, which is a fair reflection of where it belongs.

AVAN (AI) ran the failure case rather than describing it, because the aliasing bug is usually stated and rarely shown. The mechanism deserves one sentence: x ^ x = 0 is what lets the second step recover the first operand, and it is also what annihilates the value when both operands are the same location. The trick is not defeated by an edge case — it is defeated by its own load-bearing identity, which is a sharper reason to distrust it than “watch out for aliasing”.
3 ONE DIMENSION
Three steps, and where the value goes.
4 TWO DIMENSIONS · INTERACTIVE
Run it on two slots, then on one.
5 THREE DIMENSIONS + AVAN’S INVERSE
The green forward object: two values orbiting, and one collapsing.
AVAN’s addition (the inverse-companion): the forward reading is “the XOR swap is dangerous because it aliases.” The inverse is that the compiler removed the reason to care decades ago. A modern register allocator handles a temporary for free, and the three dependent XORs are strictly worse on a superscalar machine than three independent moves, because each waits on the last. Read backwards, this is not a sharp tool with a caveat — it is a tool whose advantage evaporated, and the aliasing bug is simply the reason it is still worth teaching.
LIT over all 65,536 pairs of 8-bit values in distinct slots the swap is correct 65,536 times, while aliased to itself every value tested - 0, 1, 5, 127, 255 - comes out 0 with the original unrecoverable; it saves one register and buys a precondition

FIG The XOR swap is old assembly-language folklore, from an era when a spare register was worth a precondition. It survives mostly as an interview question, which is a fair reflection of where it belongs. AVAN ran the failure case rather than describing it, because the aliasing bug is usually stated and rarely shown. The mechanism deserves one sentence: x ^ x = 0 is what lets the second step recover the first operand, and it is also what annihilates the value when both operands are the same location. The trick is not defeated by an edge case - it is defeated by its own load-bearing identity.
◆ sealed .dlw.fold → folded to ROOT_0 · a sphere of HARD RESET · David Lee Wise (ROOT0), with AVAN