Skip to content

Instantly share code, notes, and snippets.

@carlcalderon
Created January 27, 2012 09:14
Show Gist options
  • Save carlcalderon/1687901 to your computer and use it in GitHub Desktop.
Save carlcalderon/1687901 to your computer and use it in GitHub Desktop.
Bitwise options in practice (actionscript)
/*
AS3 BITWISE OPTIONS IN PRACTICE
Ever wondered how the Array.sort() does the trick?
*/
const BUY : int = 0x1; // 1
const SELL : int = 0x2; // 2
const BAKE : int = 0x4; // 4
const MUFFINS : int = 0x8; // 8
const CAKE : int = 0x10; // 16
const CHOCOLATE : int = 0x20; // 32
const VANILLA : int = 0x40; // 64
function action( flags:int ):void
{
var result : String = 'You want to ';
if( flags & BUY ) result += 'buy';
if( flags & SELL ) result += 'sell';
if( flags & BAKE ) result += 'bake';
if( flags & CHOCOLATE ) result += ' chocolate';
if( flags & VANILLA ) result += ' vanilla';
if( flags & MUFFINS ) result += ' muffins.';
if( flags & CAKE ) result += ' cake.';
trace(result);
}
action( BUY | CAKE ); // output: You want to buy cake.
action( MUFFINS | SELL ); // output: You want to sell muffins
action( BAKE | CAKE ); // output: You want to bake cake.
action( CHOCOLATE | SELL | MUFFINS ); // output: You want to sell chocolate muffins.
action( BUY | CAKE | VANILLA ); // output: You want to buy vanilla cake.
@carlcalderon
Copy link
Author

Remember that this is just an example of implementation. The #action() method does not consider conflicts such as action( BUY | SELL );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment