Paul Andrews wrote:
Jiri wrote:
When i have a 8 bit int where only one of the bit can be 1, what is then the quickest way to get that bit position with value 1?
Now I use this.

function getBit():int{
            var tCount:int = 0;
            for (var i:int=0; i < 8; i++) {
              if (8 & (1 << i))    return i;
            }
            return 0;
        }
Can this be done faster?
Yes. Use a lookup table;

var lookup:Array=[];
for (var n:int=0; n<128;n++)
{
   lookup.push(0);
}
lookup[2]=1;
lookup[4]=2;
lookup[8]=4;
//etc..

function getBit(n:int):int{
           return lookup[n];
}

LOL, better still, just ditch the function altogether and use lookup[somevariable] directly.

I might as well comment that this is lean and mean without any safety net to make sure the int is in range and indeed has only one bit set.
Paul


Jiri
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to