By Zevan | January 30, 2009
Actionscript:
-
// xor
-
trace(0 ^ 0);
-
trace(0 ^ 1);
-
trace(1 ^ 0);
-
trace(1 ^ 1);
-
-
trace("canonical representation of xor");
-
trace(xor(0, 0));
-
trace(xor(0, 1));
-
trace(xor(1, 0));
-
trace(xor(1, 1));
-
-
function xor(a:int, b:int):int{
-
//1-a is the same as int(!a)
-
return 1-a & b | a & 1-b;
-
}
-
-
/*
-
outputs:
-
0
-
1
-
1
-
0
-
canonical representation of xor
-
0
-
1
-
1
-
0
-
*/
I learned about this from reading The Elements of Computing Systems: Building a Modern Computer from First Principles By Noam Nisan and Shimon Schocken
Check out chapter 1 from the above link for an easy to understand description of the canonical representation of a boolean function.
Just a side note... this happens to be the 100th post on actionsnippet.com
Also posted in Operators | Tagged actionscript, flash |
By Zevan | January 28, 2009
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.
By Zevan | January 7, 2009
Actionscript:
-
trace(Math.pow((1+1/10000000),10000000));
-
-
trace(Math.E);
-
-
/*outputs :
-
-
2.7182816939803724
-
2.718281828459045
-
-
*/
E
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
47594571382178525166427427466391932003059921817413596629043572900334295260595630
73813232862794349076323382988075319525101901157383418793070215408914993488416750
92447614606680822648001684774118537423454424371075390777449920695517027618386062
61331384583000752044933826560297606737113200709328709127443747047230696977209310
14169283681902551510865746377211125238978442505695369677078......
Also posted in misc | Tagged actionscript, flash |