Actionscript:
-
// from http://www.bit-101.com/blog/?p=729
-
var number:Number = 10.5;
-
-
// for numbers
-
var isEven:Boolean = (number % 2) == 0;
-
trace(isEven);
-
-
// for ints
-
var integer:int = 10;
-
isEven = (integer & 1) == 0;
-
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 actionscript, flash |
By Zevan | February 9, 2009
Actionscript:
-
var obj:Object = {name: "Joe", hobby: "Guitar", age:"80"};
-
-
if ("name" in obj){
-
trace(obj, " has a name property");
-
}
-
-
if ("x" in obj == false){
-
trace(obj, " doesn't have an x property");
-
}
-
-
-
var mySprite:Sprite = new Sprite();
-
-
if ("currentFrame" in mySprite == false){
-
trace(mySprite, "does not have a currentFrame property");
-
}
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 actionscript, flash |
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 one-liners | Tagged actionscript, flash |
By Zevan | January 29, 2009
Actionscript:
-
var a:int, b:int;
-
-
a = 0;
-
b = 0;
-
-
trace(int(!(a & b)));
-
-
a = 0;
-
b = 1;
-
-
trace(int(!(a & b)));
-
-
a = 1;
-
b = 0;
-
-
trace(int(!(a & b)));
-
-
a = 1;
-
b = 1;
-
-
trace(int(!(a & b)));
-
-
/*
-
outputs:
-
1
-
1
-
1
-
0
-
*/
-
-
/*
-
NAND
-
00 1
-
01 1
-
10 1
-
11 0
-
*/
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 actionscript, flash |