I have 1 integer containing a bit mask. Various random bits will be set in the integer. It doesn't matter what the overall value of the integer is, the bits are set and cleared individually to indicate various things.
For example it might be %01101010001010010100101001000010. Bit number 0 is on the right (lowest bit), 31 is the upper bit (left).
What is a fast way, using some kind of math or binary operations and NOT a per-bit iterative search, to find the number of the lowest bit which is set (ie return 1 for the second bit and then 6 for the 7th bit etc)?
I currently do bit-tests for all bits in a while-wend loop from 0 to 31, or until a bit is found, and keep a counter of which bit number. This is obviously not a very clever algorithm. Is there some combination of and/or/not/xor/whatever which will return the lowest set bit quickly and in the same amount of time no matter how many bits there are?
For example it might be %01101010001010010100101001000010. Bit number 0 is on the right (lowest bit), 31 is the upper bit (left).
What is a fast way, using some kind of math or binary operations and NOT a per-bit iterative search, to find the number of the lowest bit which is set (ie return 1 for the second bit and then 6 for the 7th bit etc)?
I currently do bit-tests for all bits in a while-wend loop from 0 to 31, or until a bit is found, and keep a counter of which bit number. This is obviously not a very clever algorithm. Is there some combination of and/or/not/xor/whatever which will return the lowest set bit quickly and in the same amount of time no matter how many bits there are?