Faster Number rounding with bitwise operators in AS3.

as3logo
Time and time again, AS3 reaffirms its place in my heart as one of the most well planned, well implemented and functional languages I’ve had the pleasure of using. So when I came across this little gem – someone trying to perform a bitwise shift left on a floating point number – I wasn’t sure whether to be intrigued, impressed or confused.

var Y:Number = 5.5; 
var X:int = Y << 8; 

I found myself asking why this functionality was included, what would be the outcome, why it worked, and if it did work what sort of rounding  was going on behind the scenes.(Also why would anyone attempt this, but that was fairly tertiary.)

As it turns out, AS3’s SHL operator can actually be used as a very fast and quite useful form of rounding. Read on for an explanation, some examples and calculated times. 

Continue reading