It's a little curious loop!
i=0
i shifted = 0
i++ gives = 1 binary 0000 0001
i shifted = 2 binary 0000 0010
i++ gives i=3 binary 0000 0011
i shifted =6 binary 0000 0110
i++ gives i= 7 binary 0000 0111
i shifted =14 binary 0000 1110
End of loop because i>=8
i++ gives i= 15 binary 0000 1111
i shifted = 30 binary 0001 1110
John
Glen Pike wrote:
I think his i < 8 was valid as the i is used for the shift, not the
value...
The only thing that might be faster is using i-- rather than i++ - for
some reason decrementing through a loop is suppsoed to be faster.
Glen :)
John McCormack wrote:
Jiri,
I haven't done much bit twiddling yet in AS3 but I think you were
fast already, but your i was being incremented and shifted.
Also i<8 would only get you bits 2,1,0
In binary that would be i<00001000
How about:
function getBit(var numb:int):int {
var bit:int=1;
var count:int=0;
if (numb==0 || numb>0x80) return -1; // error
while (bit & numb == 0) {
1 << bit;
count++;
}
return count;
}
John
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?
_______________________________________________
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