ActionScript CodeSnippets

I was looking for some good Flash programming articles to add to by post Flash Programming – Good Articles, and came across Jim Bumgardner’s blog, which in turn led me to this solution he posted to the FlashKit forum 2-State Button.

In the examples he uses the XOR operator: ^
and the conditional operator: ?:

I’ve never been a fan of the conditional operator as the code to me, although succinct does not seem as clear as using if and else. To add to the confusion the operation is also known as Ternary operator. See the wikepedia definition Ternary_operation.
I’ll add the code from Flash help as a reference:
var timecode:String = (new Date().getHours() < 11) ? “AM” : “PM”;
trace(timecode);

The XOR operator seems like fun, although I’ve never found an instance where I could use it!

Advertisements

One thought on “ActionScript CodeSnippets

  1. I used the XOR (^) operator quite a lot more back in my C programming days, since it tends to come in handy for more low-level stuff. Not so much with Actionscript & Javascript, although it still has it’s uses.

    Along with the other bitwise operators (& and |), XOR can be useful for maintaining a set of flags in a single variable, so for example:

    kFlagRed = 0x01;
    kFlagBlue = 0x02;
    kFlagYellow = 0x04;

    curFlags = 0;

    curFlags |= kFlagRed; // set a flag
    curFlags &= ~kFlagBlue; // clear a flag
    curFlags ^= kFlagRed; // toggle a flag

    XOR has some interesting properties, which cause it to be useful for permutation. Because of this, you will often find the XOR operator lurking deep inside encryption algorithms.

    Property 1. (A XOR B) XOR B == A

    In other words, if you XOR a variable with the same constant twice, you get the original variable back.

    This property was commonly used for swapping two numbers without the need for a temporary variable.

    x ^= y;
    y ^= x;
    x ^= y;

    For integers, this has the same effect as:

    tmp = x;
    x = y ;
    y = tmp;

    But it saved a register, back when saving registers was important.

    http://en.wikipedia.org/wiki/XOR_swap_algorithm

    This property was also commonly used for rendering mouse pointers back in the days of black and white graphics. You could XOR the mouse cursor
    graphic on the screen, and then XOR it again to restore the original contents, without needing a temporary backup bitmap. This technique was the
    subject of a controversial software patent.

    An XOR trick was used in the mid-80s to maintain a doubly-linked list using only a single pointer (whereas two pointers are normally required).
    A cute trick back in the days when memory was far more precious (and questionable, due to code readability, even then).

    http://en.wikipedia.org/wiki/XOR_linked_list

    Property 2. For any unique set of numbers A,B,C…N, if you XOR each of these numbers with a constant c, the resulting set of numbers will also be unique.

    If the set of numbers is continuous, and the resulting XORed numbers don’t produce numbers outside of the original range, you can use this
    trick to permute the order. For example, you can XOR all the numbers from 0-255 with 0xAA to produce the same numbers, in a different ordering.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s