b-log – betriebsraum weblog

Software Development, Human-Computer Interaction, Projects…

Binary arithmetic with static variables

October 20, 2005

For many of you this is probably nothing new but in case you didn’t know or used this, maybe you’ll find it useful:

Let’s say you have a function in your application that displays some buttons and you want to display different configurations/arrangements of these buttons each time depending on some condition. For example you have some forms for login, register or edit and on the login screen you want to show a login button, register button and forgot password button. On the register screen you want only a submit button and a back/cancel button. Have a look at this example class:

class ButtonTest {	
 
	public static var SUBMIT:Number = 1;
	public static var CANCEL:Number = 2;
	public static var OPTION:Number = 4;
	public static var ALL:Number = 7;	
 
	private var container_mc:MovieClip;
	private var lastX:Number;	
 
	public function ButtonTest(target:MovieClip, depth:Number) {
		container_mc = target.createEmptyMovieClip("container_mc"+depth, depth);
	}	
 
	public function show(buttons:Number):Void {
		lastX = 0;		
 
		if (buttons & SUBMIT) {
			createButton("submit", lastX);
		}		
 
		if (buttons & CANCEL) {
			createButton("cancel", lastX);
		}		
 
		if (buttons & OPTION) {
			createButton("option", lastX);
		}
	}	
 
	private function createButton(type:String, x:Number):Void {
		var button:MovieClip = container_mc.attachMovie("mc_"+type, type+"_mc", container_mc.getNextHighestDepth());
		button._x = x;
		lastX = button._x + button._width + 10;
	}	
 
}

To test this code, create a submit, cancel and option button and give it linkage ids of “mc_submit”, “mc_cancel” and “mc_option”. In your fla-file write:

var bt:ButtonTest = new ButtonTest(this, 1);
bt.show(ButtonTest.SUBMIT);

Now you should see a submit button. Now try:

bt.show(ButtonTest.SUBMIT | ButtonTest.CANCEL);

This should display the submit and cancel button. And now try:

bt.show(ButtonTest.ALL &~ Button.OPTION);

This should also display the submit and cancel button. And last try:

bt.show(ButtonTest.ALL);

This sould display all buttons. You may have seen something similar in the macromedia alert component to display the ok and cancel buttons (Alert.OK and Alert.CANCEL) or in php custom error handling (E_USER_ERROR, E_WARNING etc.).

How does it work? Actually it’s very simple binary arithmetic. If you combine two “constants” (there are no real constants in AS2) with a bitwise OR (|) you get an unique number which is the sum of the single values. For example the result of ButtonTest.SUBMIT | ButtonTest.CANCEL is 3.

1 (ButtonTest.SUBMIT) corresponds to binary: 001
2 (ButtonTest.CANCEL) corresponds to binary: 010
Bitwise OR operation:
001
010
—–
011 (= 3 in decimal).

The if-conditions us the bitwise AND to check which button to display:
if (buttons & Button.SUBMIT) {…}. In this case the if-condition is true because buttons corresponds to binary: 011 (as seen above)
ButtonTest.SUBMIT corresponds to binary: 001
Bitwise AND operation:
011
001
—–
001 (= 1 in decimal).

You can check this if you put a trace in the show function:

trace(buttons & ButtonTest.SUBMIT);

1 (and every other possible result except 0) is always true. If you traced out (buttons & Buttons.OPTION) the result will be 0, the if-condition is false and so no button is created. It’s important that every static variable has a value that is a power of 2 (1, 2, 4, 8 etc.) so there’s always a unique combination.

If you want to learn more about binary arithmetic and bitwise operations here a two useful links:

This technique is pretty useful in flash for situations where you want to show or hide certain gui elements depending on the current state of the application (user logged in/logged out, user is admin etc.). The advantage is that you can have many different options and combinations with one single parameter. Other common applications of bitwise operations include the management of security options, which provide different types of users with different levels of access to an application (think of a simple example where the constants of the ButtonTest class would represent states like VIEW, ADD, EDIT, DELETE etc.), performance improvements in game programming and programming under restrictions of memory or disk space.

Filed under: Flex/AS3, Software Development

2 Responses to “Binary arithmetic with static variables”

  1. You can also do this using enums as bitflags, which unfortunately are not natively available in Actionscript – you can, however, simulate them, as described @ http://blog.jasonnussbaum.com/?p=48

  2. felix says:

    interesting article…thanks!

Add a comment

Follow comments to this post by subscribing to the comment feed.