Category Archives: one-liners

Canonical Representation of XOR

Actionscript:
  1. // xor
  2. trace(0 ^ 0);
  3. trace(0 ^ 1);
  4. trace(1 ^ 0);
  5. trace(1 ^ 1);
  6.  
  7. trace("canonical representation of xor");
  8. trace(xor(0, 0));
  9. trace(xor(0, 1));
  10. trace(xor(1, 0));
  11. trace(xor(1, 1));
  12.  
  13. function xor(a:int, b:int):int{
  14.     //1-a   is the same as   int(!a)
  15.     return 1-a & b | a & 1-b;
  16. }
  17.  
  18. /*
  19. outputs:
  20. 0
  21. 1
  22. 1
  23. 0
  24. canonical representation of xor
  25. 0
  26. 1
  27. 1
  28. 0
  29. */

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 , | 2 Comments

Variable Swap

Actionscript:
  1. //
  2. // swap some variables
  3. // all techniques except the first are from http://cpptruths.blogspot.com/2006/04/swapping-two-integers-in-one-liner.html
  4. //
  5. var a:Number = 1.1;
  6. var b:Number= 2.2;
  7.  
  8. trace(a, b);
  9.  
  10. // best, fastest, easiest to read way
  11. var t:Number= a;
  12. a = b;
  13. b = t;
  14.  
  15. trace(a, b);
  16.  
  17. // not recommended slower ways:
  18.  
  19. b=a+b-(a=b);
  20.  
  21. trace(a, b);
  22.  
  23. // xor versions will only work with ints and uints
  24. trace("\nxor kills decimals:");
  25.  
  26. // easy to understand xor version
  27. a^=b;
  28. b^=a;
  29. a^=b;
  30.  
  31. trace(a, b);
  32.  
  33. // one line xor version
  34.  
  35. a=(b=(a=b^a)^b)^a;
  36.  
  37. trace(a, b);
  38.  
  39. /* outputs:
  40. 1.1 2.2
  41. 2.2 1.1
  42. 1.1 2.2
  43.  
  44. xor kills decimals:
  45. 2 1
  46. 1 2
  47. */

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.

Also posted in Operators, variables | Tagged , | Leave a comment

Zeno’s [classic easeOut]

Actionscript:
  1. stage.frameRate = 30;
  2. var circle:Shape = Shape(addChild(new Shape()));
  3. with(circle.graphics) beginFill(0x000000), drawCircle(0,0,10);
  4. circle.y = 50;
  5.  
  6. addEventListener(Event.ENTER_FRAME, onLoop);
  7. function onLoop(evt:Event):void {
  8.     circle.x += (mouseX - circle.x) / 12;
  9. }

Also posted in motion | Tagged , | Leave a comment

E?

Actionscript:
  1. trace(Math.pow((1+1/10000000),10000000));
  2.  
  3. trace(Math.E);
  4.  
  5. /*outputs :
  6.  
  7. 2.7182816939803724
  8. 2.718281828459045
  9.  
  10. */

E

2.7182818284590452353602874713526624977572470936999595749669676277240766303535
47594571382178525166427427466391932003059921817413596629043572900334295260595630
73813232862794349076323382988075319525101901157383418793070215408914993488416750
92447614606680822648001684774118537423454424371075390777449920695517027618386062
61331384583000752044933826560297606737113200709328709127443747047230696977209310
14169283681902551510865746377211125238978442505695369677078......

Also posted in misc | Tagged , | Comments closed