{"id":477,"date":"2012-06-27T23:13:33","date_gmt":"2012-06-27T23:13:33","guid":{"rendered":"https:\/\/halsafar.ca\/blog\/?p=477"},"modified":"2015-08-26T10:28:35","modified_gmt":"2015-08-26T16:28:35","slug":"swap-with-no-temp-variable","status":"publish","type":"post","link":"https:\/\/halsafar.ca\/blog\/2012\/06\/27\/swap-with-no-temp-variable\/","title":{"rendered":"Swap with no Temp Variable"},"content":{"rendered":"<p>When I used to TA for the University of Saskatchewan in our intro C\/C++ first year classes I used to try and challenge the students sometimes. \u00a0When the assignment rolled out on sorting the students were instructed to swap two variables. \u00a0As brand new programmers sometimes something as simple as swapping two variables doesn&#8217;t leap out at you. \u00a0Of course we just teach them that you need a temp variable and mission accomplished. \u00a0Well I always offered bonus marks for anyone who could derive the variable swap with no temp variable. \u00a0 Years pass, no student ever got back to me with the answer ( they obviously didn&#8217;t have Google Fu ). \u00a0I find myself writing a routine today that required a swap. \u00a0I still remembered the trick:<\/p>\n<p style=\"padding-left: 30px;\">int main()<br \/>\n{<\/p>\n<p style=\"padding-left: 60px;\">int x = 42;<br \/>\nint y = 51236;<\/p>\n<p style=\"padding-left: 60px;\">printf(&#8220;X before swap = %d\\n&#8221;, x);<\/p>\n<p style=\"padding-left: 60px;\">x ^= y;<br \/>\ny ^= x;<br \/>\nx ^= y;<\/p>\n<p style=\"padding-left: 60px;\">printf(&#8220;X after swap = %d\\n&#8221;, x);<\/p>\n<p style=\"padding-left: 60px;\">return 0;<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>XOR god mode and fairly easy to remember. X xor Y, Y xor X, X xor Y. \u00a0Useful for any POD that is expressed as bytes consistently across architectures.<\/p>\n<p>Needless to say this is a fun trick to blow the minds of long time programmers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I used to TA for the University of Saskatchewan in our intro C\/C++ first year classes I used to try and challenge the students sometimes. \u00a0When the assignment rolled out on sorting the students were instructed to swap two<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/halsafar.ca\/blog\/2012\/06\/27\/swap-with-no-temp-variable\/\">Read more <span class=\"screen-reader-text\">Swap with no Temp Variable<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[13,22,67,66,68],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-developement","category-halsafar","tag-development","tag-linkedin","tag-performance","tag-programming","tag-swap"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":7,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/posts\/477\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/halsafar.ca\/blog\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}