[Flashcoders] bitwise question

2009-09-15 Thread Jiri
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

Re: [Flashcoders] bitwise question

2009-09-15 Thread Hans Wichman
Hey, not sure if it's faster, but this would work as well: var i:Number = 32; trace (Math.log(i)/Math.LN2); returns bit number 5. Lookuptable might be faster in this case. greetz JC On Tue, Sep 15, 2009 at 10:04 AM, Jiri jiriheitla...@googlemail.com wrote: When i have a 8 bit int where only

Re: [Flashcoders] bitwise question

2009-09-15 Thread Paul Andrews
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

Re: [Flashcoders] bitwise question

2009-09-15 Thread Paul Andrews
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

Re: [Flashcoders] bitwise question

2009-09-15 Thread Jiri
Thnx, i tought that there would be some kind of smart bitwise operation to do that. This will do fine. Cheers. Jiri Paul Andrews wrote: 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

Re: [Flashcoders] bitwise question

2009-09-15 Thread Juan Pablo Califano
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

Re: [Flashcoders] bitwise question

2009-09-15 Thread Paul Andrews
Juan Pablo Califano wrote: 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:

Re: [Flashcoders] bitwise question

2009-09-15 Thread John McCormack
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 i8 would only get you bits 2,1,0 In binary that would be i1000 How about: function getBit(var numb:int):int { var bit:int=1; var count:int=0; if

Re: [Flashcoders] bitwise question

2009-09-15 Thread Glen Pike
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

Re: [Flashcoders] bitwise question

2009-09-15 Thread John McCormack
It's a little curious loop! i=0 i shifted = 0 i++ gives = 1 binary 0001 i shifted = 2 binary 0010 i++ gives i=3 binary 0011 i shifted=6 binary 0110 i++ gives i= 7 binary 0111 i shifted=14 binary 1110 End of loop because i=8 i++ gives i= 15 binary

Re: [Flashcoders] bitwise question

2009-09-15 Thread Jiri
Can't confirm your findings right now, and also wonder which loop you are talking about. My initially posted one? function getBit():int{ var tCount:int = 0; for (var i:int=0; i 8; i++) { if (8 (1 i))return i; } return 0;

Re: [Flashcoders] bitwise question

2009-09-15 Thread Jiri
Cheers. Jiri Paul Andrews wrote: Juan Pablo Califano wrote: 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;