Blog › Bitwise Shifting

Hi I'm Fraser, a Senior Developer at Lean Mean Fighting Machine specialising in Actionscript but looking into a range of technologies.

email: me@fraserhobbs.com

Platform

Language

Category

Archives

Bitwise Shifting

Bitwise shifting can be useful in AS3 as it can give some quite significant performance gains. It isn’t actually as complicated as you think and the name explains exactly what’s going on.

If we take the number 7 represented as 111 in binary and we move that value right and so removing the last 1 we get 11 which is binary for 3. Results are always floored because we are dealing in binary and so whole numbers.

Here are a few examples of it can be used:

Divide by 2 and floor

num >> 1

Multiply by 2 and floor

num << 1

Split up 24bit RGB Colour

var colour:uint = 0x336699;
var red:uint = color >> 16;
var green:uint = color >> 8 & 0xFF;
var blue:uint = color & 0xFF;