TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: History of XOR Swapping

6 pointsby kilodecaalmost 4 years ago
Do you know about the first documented use of XOR to swap values of two variables?

5 comments

dalkealmost 4 years ago
I took a look in the EDSAC manual (&quot;The Preparation of Programs for an Electronic Digital Computer&quot;, 1951) at <a href="https:&#x2F;&#x2F;archive.org&#x2F;details&#x2F;programsforelect00wilk&#x2F;page&#x2F;154&#x2F;mode&#x2F;2up?q=sideways" rel="nofollow">https:&#x2F;&#x2F;archive.org&#x2F;details&#x2F;programsforelect00wilk&#x2F;page&#x2F;154&#x2F;...</a> - linking to the earliest popcount description, Gillies-Miller.<p>I didn&#x27;t see anything.<p>Then again, that hardware doesn&#x27;t seem to support xor. Nor does the manual mention &quot;Boolean&quot; or &quot;Booleian&quot; (the latter was used in the older literature, see <a href="https:&#x2F;&#x2F;scholar.google.se&#x2F;scholar?hl=sv&amp;as_sdt=0%2C5&amp;q=Booleian&amp;btnG=" rel="nofollow">https:&#x2F;&#x2F;scholar.google.se&#x2F;scholar?hl=sv&amp;as_sdt=0%2C5&amp;q=Boole...</a> for examples ).<p>I looked for, but didn&#x27;t find, a description of it in HAKMEM (1972).<p>Those two exhaust my knowledge of reference materials for that era.
评论 #27622041 未加载
kazinatoralmost 4 years ago
Try the Retrocomputing StackExchange, by the way.<p>Here is smoething useful. I just randomly picked the IBM 704 (famous for early Lisp work) and researched the architecture. I found this site:<p><a href="https:&#x2F;&#x2F;sky-visions.com&#x2F;ibm&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sky-visions.com&#x2F;ibm&#x2F;</a><p>It has info the 704 and other higher model numbers after that. Whereas no exclusive OR instruction is listed for the 704, it looks like 709 got one: opcode ERA, exclusive OR to accumulator. The 709 was introduced in 1957.<p>OK, we have identified a machine. Next we head to the Software Preservation Group for any code for the 709.<p>E.g. here is some code that uses ERA, but not for swapping:<p><a href="http:&#x2F;&#x2F;www.softwarepreservation.org&#x2F;projects&#x2F;FORTRAN&#x2F;source&#x2F;ibsys&#x2F;FORTRAN&#x2F;9F06.fap&#x2F;?searchterm=709" rel="nofollow">http:&#x2F;&#x2F;www.softwarepreservation.org&#x2F;projects&#x2F;FORTRAN&#x2F;source&#x2F;...</a><p>Looks like ERA was popular in 709 systems sources as a comparison operator: load a value into A, then XOR with some constant, and if it&#x27;s zero, there is a match. I&#x27;ve spotted quite a few uses like that.
HelloNursealmost 4 years ago
The XOR swapping trick is possible since the earliest machine languages with memory to memory XOR operations (or, less simply, any XOR operation), so it&#x27;s unlikely to be seriously documented until someone began teaching it.
sloakenalmost 4 years ago
Seems like an old interest.<p>I will ask an older friend when he first learned of it.<p>He said it was in college in the 70&#x27;s. It was taught as a method for having a doubly linked list, but only use the space for one link. He thinks it was in an data structures book by hororwitz (not sure of spelling obviously)
评论 #27624524 未加载
brudgersalmost 4 years ago
Related, XOR Swap Algorithm, <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;XOR_swap_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;XOR_swap_algorithm</a>