{"id":170,"date":"2009-01-28T00:00:28","date_gmt":"2009-01-28T07:00:28","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=619"},"modified":"2009-01-28T00:00:28","modified_gmt":"2009-01-28T07:00:28","slug":"variable-swap","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=170","title":{"rendered":"Variable Swap"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/\r\n\/\/ swap some variables\r\n\/\/ all techniques except the first are from http:\/\/cpptruths.blogspot.com\/2006\/04\/swapping-two-integers-in-one-liner.html\r\n\/\/\r\nvar a:Number = 1.1;\r\nvar b:Number= 2.2;\r\n\r\ntrace(a, b);\r\n\r\n\/\/ best, fastest, easiest to read way\r\nvar t:Number= a;\r\na = b;\r\nb = t;\r\n\r\ntrace(a, b);\r\n\r\n\/\/ not recommended slower ways:\r\n\r\nb=a+b-(a=b);\r\n\r\ntrace(a, b);\r\n\r\n\/\/ xor versions will only work with ints and uints\r\ntrace(&quot;nxor kills decimals:&quot;);\r\n\r\n\/\/ easy to understand xor version\r\na^=b;\r\nb^=a;\r\na^=b;\r\n\r\ntrace(a, b);\r\n\r\n\/\/ one line xor version\r\n\r\na=(b=(a=b^a)^b)^a;\r\n\r\ntrace(a, b);\r\n\r\n\/* outputs:\r\n1.1 2.2\r\n2.2 1.1\r\n1.1 2.2\r\n\r\nxor kills decimals:\r\n2 1\r\n1 2\r\n*\/\r\n<\/pre>\n<p>The above swaps variables a and b in a few different ways. The first way (using a temp variable) is the best and fastest way&#8230; the rest of the ways are just interesting and fun. <\/p>\n<p>I was coding and something reminded me that there are obscure variable swapping techniques out there&#8230; so I figured I&#8217;d google for a bit&#8230;. there are tons of examples of these online &#8211; with lots of good explanations&#8230;. I got the above from <a href=\"http:\/\/cpptruths.blogspot.com\/2006\/04\/swapping-two-integers-in-one-liner.html\">this link<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ \/\/ swap some variables \/\/ all techniques except the first are from http:\/\/cpptruths.blogspot.com\/2006\/04\/swapping-two-integers-in-one-liner.html \/\/ var a:Number = 1.1; var b:Number= 2.2; trace(a, b); \/\/ best, fastest, easiest to read way var t:Number= a; a = b; b = t; trace(a, b); \/\/ not recommended slower ways: b=a+b-(a=b); trace(a, b); \/\/ xor versions will only [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,36,68],"tags":[3,4],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-one-liners","category-operators","category-variables","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=170"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/170\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}