Another option: function test(value:int):int { // just to be safe... value = value & 0xff; switch(value) { case 0x01: return 0; case 0x02: return 1; case 0x04: return 2; case 0x08: return 3; case 0x10: return 4; case 0x20: return 5; case 0x40: return 6; case 0x80: return 7; default: return -1; } }
Cheers Juan Pablo Califano 2009/9/15 Paul Andrews <p...@ipauland.com> > 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 > _______________________________________________ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders