Category Archives: Operators

Odd Even Classic

Actionscript:
  1. // from http://www.bit-101.com/blog/?p=729
  2. var number:Number = 10.5;
  3.  
  4. // for numbers
  5. var isEven:Boolean = (number % 2) == 0;
  6. trace(isEven);
  7.  
  8. // for ints
  9. var integer:int = 10;
  10. isEven = (integer & 1) == 0;
  11. trace(isEven);

This is a classic that I've seen all over the place online... found myself needing it today and figured I should post it. It just an easy way to determine if a number is odd or even... I remember originally reading it over at Bit-101.com a few years back...

Two posts today because I think I missed a post recently...

Also posted in Math | Tagged , | Leave a comment

“in” Operator

Actionscript:
  1. var obj:Object = {name: "Joe", hobby: "Guitar", age:"80"};
  2.  
  3. if ("name" in obj){
  4.     trace(obj, " has a name property");
  5. }
  6.  
  7. if ("x" in obj == false){
  8.     trace(obj, " doesn't have an x property");
  9. }
  10.  
  11.  
  12. var mySprite:Sprite = new Sprite();
  13.  
  14. if ("currentFrame" in mySprite == false){
  15.     trace(mySprite, "does not have a currentFrame property");
  16. }

The "in" operator can be used to check if an object has a specific property. In the above example I test a standard Object instance and a Sprite instance for a few different properties.

I could see this operator being used when dealing with * (wildcard) datatypes and dynamic style code....

Also posted in dynamic | Tagged , | Leave a comment

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 one-liners | Tagged , | 2 Comments

NAND

Actionscript:
  1. var a:int, b:int;
  2.  
  3. a = 0;
  4. b = 0;
  5.  
  6. trace(int(!(a & b)));
  7.  
  8. a = 0;
  9. b = 1;
  10.  
  11. trace(int(!(a & b)));
  12.  
  13. a = 1;
  14. b = 0;
  15.  
  16. trace(int(!(a & b)));
  17.  
  18. a = 1;
  19. b = 1;
  20.  
  21. trace(int(!(a & b)));
  22.  
  23. /*
  24. outputs:
  25. 1
  26. 1
  27. 1
  28. 0
  29. */
  30.  
  31. /*
  32. NAND
  33. 00    1
  34. 01    1
  35. 10    1
  36. 11    0
  37. */

I started reading "The Elements of Computing Systems: Building a Modern Computer from First Principles" By Noam Nisan and Shimon Schocken. So far it's a very fun read. They talk about the power of NAND in the first chapter....

Also posted in misc | Tagged , | Leave a comment