Just saw a great post over here: http://www.coldfusioncookbook.com/entry/53/How-do-I-determine-if-a-number-is-even-or-odd?
Actually, it was the first comment that was awesome. Were I to decide if a number is odd or even in ActionScript, I’d do the same thing the original post did (translated to AS):
iseven = ((num % 2) == 0)
The comment suggested a faster way using bitwise operators. Translated to AS it’s this:
iseven = ((num & 1) == 0)
Makes sense when you look at binary numbers:
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
Each of the odd numbers has a 1 in the far right column. The even numbers have 0 there. Mask it with “& 1” and you can see which it is.
A pretty minor difference syntactically, but bitwise operators are FAST. I’m not sure how mod (%) works internally, but I’d guess it does a fair amount of calculation.
Nice elegant solution.