Actionscript:
-
//
-
// 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 work with ints and uints
-
trace("\nxor kills decimals:");
-
-
// easy to understand xor version
-
a^=b;
-
b^=a;
-
a^=b;
-
-
trace(a, b);
-
-
// one line xor version
-
-
a=(b=(a=b^a)^b)^a;
-
-
trace(a, b);
-
-
/* outputs:
-
1.1 2.2
-
2.2 1.1
-
1.1 2.2
-
-
xor kills decimals:
-
2 1
-
1 2
-
*/
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... the rest of the ways are just interesting and fun.
I was coding and something reminded me that there are obscure variable swapping techniques out there... so I figured I'd google for a bit.... there are tons of examples of these online - with lots of good explanations.... I got the above from this link.