[Flashcoders] bitwise question
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? Jiri ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] bitwise question
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 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? 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
Re: [Flashcoders] bitwise question
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; n128;n++) { lookup.push(0); } lookup[2]=1; lookup[4]=2; lookup[8]=4; //etc.. function getBit(n:int):int{ return lookup[n]; } 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
Re: [Flashcoders] bitwise question
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; n128;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
Re: [Flashcoders] bitwise question
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 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; n128;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
Re: [Flashcoders] bitwise question
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; n128;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
Re: [Flashcoders] bitwise question
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: return 6; case 0x80: return 7; default: return -1; } } Cheers Juan Pablo Califano Yes, this is the best way to do it. Paul ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] bitwise question
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 (numb==0 || numb0x80) 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
Re: [Flashcoders] bitwise question
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 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 (numb==0 || numb0x80) 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
Re: [Flashcoders] bitwise question
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 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 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 (numb==0 || numb0x80) 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
Re: [Flashcoders] bitwise question
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; } or the one suggested by John McCormack. I cant remember have faulty result with my loop because like Glen Pike mentioned the i is used for the shifting. Also the 8bit to test will always only have one bit set to 1. Which could be a potential dangerous assumption i guess. 0001 0010 0100 etc.. Will do testing tomorrow. jiri John McCormack wrote: 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 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 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 (numb==0 || numb0x80) 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 ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] bitwise question
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; case 0x20: return 5; case 0x40: return 6; case 0x80: return 7; default: return -1; } } Cheers Juan Pablo Califano Yes, this is the best way to do it. Paul ___ 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