{"id":1494,"date":"2019-03-09T15:30:23","date_gmt":"2019-03-09T23:30:23","guid":{"rendered":"http:\/\/wonghoi.humgar.com\/blog\/?p=1494"},"modified":"2019-03-09T20:13:51","modified_gmt":"2019-03-10T04:13:51","slug":"explain-xor-tricks-succinctly-with-two-properties","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2019\/03\/09\/explain-xor-tricks-succinctly-with-two-properties\/","title":{"rendered":"Explain XOR tricks succinctly with two properties"},"content":{"rendered":"<p>Most teaching materials on XOR starts from its straight definition then a bunch of recipes for exploiting XOR. It is not convenient to remember and not intuitive to come up with new tricks on our own. Instead, I&#8217;d like to describe XOR as two intuitive operations:<\/p>\n<ol>\n<li>Change detector (The logic definition itself)<\/li>\n<li>Toggling light switches (Flipping twice undoes it)<\/li>\n<\/ol>\n<p>The second interpretation (toggling) is actually way more powerful and easy to remember than the first interpretation (definition).<\/p>\n<hr \/>\n<p>With the toggling interpretation, you don&#8217;t have to think hard to come up with the four algebraic properties:<\/p>\n<ol>\n<li><em>Associativity<\/em> and <em>commutativity<\/em>: we only care about whether the individual light switches (bits) are flipped odd (flipped) or even (not-flipped) number of times at the end of the day. When it happens in what order does not matter.<\/li>\n<li><em>Identity<\/em>: flipping NONE of the switches (a mask of all 0) leaves it alone<\/li>\n<li><em>Inverse<\/em>: pick only the switches that are already turned on (using itself as mask) and flip them (xor) guarantees to turn everything off (0)<\/li>\n<\/ol>\n<hr \/>\n<p>The XOR swap trick can be constructed by exploiting the fact that flipping the same switch twice (at any point) reverts it back to where it started:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 25%;\"><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-7e5fbfa0bbbd9f3051cd156a0f1b5e31_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;\" title=\"Rendered by QuickLaTeX.com\" height=\"8\" width=\"10\" style=\"vertical-align: 0px;\"\/><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-38461fc041e953482219abf5d4cce1cb_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/><\/td>\n<td style=\"width: 25%;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Start<\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"17\" style=\"vertical-align: -3px;\"\/><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/><\/td>\n<td style=\"width: 25%;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Mix <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> with <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"17\" style=\"vertical-align: -3px;\"\/><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-69a298038cf510c6ca9a90bd55441d52_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#49;&#32;&#61;&#32;&#120;&#95;&#48;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"97\" style=\"vertical-align: -4px;\"\/><\/td>\n<td style=\"width: 25%;\"><\/td>\n<td style=\"width: 25%;\">No info is lost, still have <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> somewhere to undo if we wish.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">The incumbent <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> is used to cancel the <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> inside <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-d7aa45c8899989487fb32dab51a8f7d7_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"16\" style=\"vertical-align: -3px;\"\/> to recover <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"17\" style=\"vertical-align: -3px;\"\/><\/td>\n<td style=\"width: 25%;\"><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-288dc1d13d81f4cc720d99941b81f863_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#123;&#114;&#99;&#108;&#125; &#121;&#95;&#50;&#32;&#38;&#32;&#61;&#32;&#38;&#32;&#121;&#95;&#48;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#120;&#95;&#49;&#32;&#92;&#92; &#38;&#32;&#61;&#32;&#38;&#32;&#121;&#95;&#48;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#40;&#120;&#95;&#48;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#121;&#95;&#48;&#41;&#92;&#92; &#38;&#32;&#61;&#32;&#38;&#32;&#120;&#95;&#48; &#92;&#101;&#110;&#100;&#123;&#97;&#114;&#114;&#97;&#121;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"58\" width=\"169\" style=\"vertical-align: -25px;\"\/><\/td>\n<td style=\"width: 25%;\">Since <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> is already saved (mixed with) <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-d7aa45c8899989487fb32dab51a8f7d7_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"16\" style=\"vertical-align: -3px;\"\/>, we don&#8217;t have to worry about losing it as long as we get to our main goal of putting <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"17\" style=\"vertical-align: -3px;\"\/> in <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-38461fc041e953482219abf5d4cce1cb_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/>, which will be used as a recovery key later.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 25%;\">Now use the <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-e6e1c3611728e9db0e52bb6485e06068_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"17\" style=\"vertical-align: -3px;\"\/> stored in <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-66add1263723a25fcd4d3229e03d83a4_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#50;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/> to unmix <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-d7aa45c8899989487fb32dab51a8f7d7_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#49;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"16\" style=\"vertical-align: -3px;\"\/> to recover <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-2b1ce6fe7664ef728c0428024c69737b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#48;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"16\" style=\"vertical-align: -4px;\"\/><\/td>\n<td style=\"width: 25%;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/ql-cache\/quicklatex.com-abbb82774cfed520b6d19c8593343927_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#123;&#114;&#99;&#108;&#125; &#120;&#95;&#51;&#32;&#38;&#32;&#61;&#32;&#38;&#32;&#120;&#95;&#49;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#121;&#95;&#50;&#32;&#92;&#92; &#38;&#32;&#61;&#32;&#38;&#32;&#40;&#120;&#95;&#48;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#121;&#95;&#48;&#41;&#32;&#92;&#111;&#112;&#108;&#117;&#115;&#32;&#120;&#95;&#48;&#92;&#92; &#38;&#32;&#61;&#32;&#38;&#32;&#121;&#95;&#48; &#92;&#101;&#110;&#100;&#123;&#97;&#114;&#114;&#97;&#121;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"59\" width=\"172\" style=\"vertical-align: -26px;\"\/><\/td>\n<td style=\"width: 25%;\"><\/td>\n<td style=\"width: 25%;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note that this trick is obsolete for modern computer architecture because<\/p>\n<ul>\n<li>it enforces strict dependence that kills any predictive pipeline (speedup) in modern computer architecture, and<\/li>\n<li>will yield zero all the time if you try to swap with itself <strong>in-place<\/strong> (same memory location), which is wrong because you expect the swap to do nothing.<\/li>\n<\/ul>\n<p>Adding a check for the degenerate case (self-swap) slows the program down even further, making it worse than using a temporary storage for swapping. Therefore there&#8217;s no good reason to use it unless you have an exotic use case.<\/p>\n<p>This is mainly used as a teaching device (or homework problem) to teach that XOR-ing even instances of the same variable in the chain cancels it.<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1494\" class=\"pvc_stats all  \" data-element-id=\"1494\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Most teaching materials on XOR starts from its straight definition then a bunch of recipes for exploiting XOR. It is not convenient to remember and not intuitive to come up with new tricks on our own. Instead, I&#8217;d like to &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2019\/03\/09\/explain-xor-tricks-succinctly-with-two-properties\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1494\" class=\"pvc_stats all  \" data-element-id=\"1494\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1494","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":18,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"predecessor-version":[{"id":1516,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/1494\/revisions\/1516"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}