{"id":5080,"date":"2023-04-24T23:53:12","date_gmt":"2023-04-25T04:53:12","guid":{"rendered":"https:\/\/www.paleotechnologist.net\/?p=5080"},"modified":"2023-04-24T23:53:12","modified_gmt":"2023-04-25T04:53:12","slug":"the-old-xor-switcheroo","status":"publish","type":"post","link":"https:\/\/www.paleotechnologist.net\/?p=5080","title":{"rendered":"The Old XOR Switcheroo"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Suppose you have two binary integers A and B.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You want to swap them so that A becomes B and B becomes A, but you don&#8217;t have any extra memory. If you start with <strong>A=B<\/strong> , for instance, the information that was in A is lost.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Can this be done? Surprisingly, yes, using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/XOR_swap_algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">XOR swap <\/a>algorithm:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A = A XOR B;<\/strong><br><strong>B = B XOR A;<\/strong><br><strong>A = A XOR B;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A is first changed to be a bitwise XOR of itself and B. As long as B is still available, the information in A could be recovered by repeating the operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, B is XORed with the new value of A. This leaves B with the information that was originally in A. Since A still contains the combination, both pieces of information are still recoverable. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, A is XORed with B. Since B contains the original value that was in A, A will now contain the other half of the information &#8212; the value originally in B.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, for the story behind the story&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The idea, once known, is easy enough to prove &#8212; but I wanted to know who had come up with it. I tried asking ChatGPT, and it knew exactly the algorithm I was talking about, and rephrased what it does. It then very confidently said that it was discovered by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Richard_Hamming\" target=\"_blank\" rel=\"noreferrer noopener\">Richard Hamming<\/a> in the 1950s, and gave textbook and paper citations of his to back it up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Very impressive &#8212; except it seems to be a hallucination. Maybe Hamming <em>did<\/em> discover the XOR swap. It wouldn&#8217;t surprise me at all. (After all, he&#8217;s the Hamming in &#8220;Hamming codes.&#8221; But the two references ChatGPT gave were duds. Hamming does mention XOR in his book on communications, but only briefly, where it is relevant to error-correcting codes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swap two binary integers, without using a temporary variable. Not magic &#8212; XOR. <a href=\"https:\/\/www.paleotechnologist.net\/?p=5080\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"footnotes":""},"categories":[530,263,216,210,222],"tags":[532,751,749,750],"class_list":["post-5080","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-assembly","category-coding","category-digital","category-math","tag-algorithms","tag-boolean-algebra","tag-xor","tag-xor-swap"],"_links":{"self":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/5080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5080"}],"version-history":[{"count":2,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/5080\/revisions"}],"predecessor-version":[{"id":5082,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=\/wp\/v2\/posts\/5080\/revisions\/5082"}],"wp:attachment":[{"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.paleotechnologist.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}