Category Archives: Operators

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 one-liners, variables | Tagged , | Leave a comment

XOR Color Invert

Actionscript:
  1. var yellow:uint = 0xFFFF00;
  2.  
  3. // draws yellow circle
  4. with(graphics) beginFill(yellow), drawCircle(100,100,50);
  5.  
  6. // invert the color using XOR assignment
  7. // yellow becomes 0x0000FF
  8. yellow ^= 0xFFFFFF;
  9.  
  10. // draws blue  circle
  11. with(graphics) beginFill(yellow), drawCircle(200,100,50);

Just a fun use for XOR. You could also do it without XOR assignment:

Actionscript:
  1. with(graphics) beginFill(yellow ^ 0xFFFFFF), drawCircle(200,100,50);

Playing a little with bitwise operators is a good way to make sure you understand them. Try tweaking this trace statement:

Actionscript:
  1. trace((0x543210 ^ 0xFFFFFF).toString(16));

If you'd like to read about bitwise operators, I recommend wikipedia's article.

Also posted in one-liners | Tagged , , | Leave a comment

Toggle .visible

Actionscript:
  1. // toggle a DisplayObject's visible property
  2. var shape = new Shape();
  3.  
  4. shape.visible = !shape.visible;
  5. trace(shape.visible); // outputs false
  6.  
  7. shape.visible = !shape.visible;
  8. trace(shape.visible); // outputs true
  9.  
  10. shape.visible = !shape.visible;
  11. trace(shape.visible); // outputs false
  12.  
  13. shape.visible = !shape.visible;
  14. trace(shape.visible);  // outputs true

This is pretty obvious to anyone with a full understanding of the ! operator. It's useful for things
like checkboxes and other types of toggle buttons. The first time I ever encountered this technique
was in processing source... in one of the demos on toxi.co.uk

Here's another quick example you can run in your timeline:

Actionscript:
  1. var circle:Shape =  new Shape();
  2. circle.graphics.beginFill(0xFF0000);
  3. circle.graphics.drawCircle(0, 0,10);
  4. circle.x = 80;
  5. circle.y = 105;
  6. addChild(circle)
  7.  
  8. var btn:TextField = new TextField();
  9. btn.text = "click this text to toggle red circle's visibility";
  10. btn.x = btn.y = 100;
  11. btn.selectable = false;
  12. btn.border = true;
  13. btn.autoSize = TextFieldAutoSize.LEFT;
  14. addChild(btn);
  15.  
  16. btn.addEventListener(MouseEvent.CLICK, onClick);
  17. function onClick(evt:MouseEvent):void{
  18.     circle.visible = !circle.visible;
  19. }

Because "not false" is true.

Posted in Operators | Leave a comment