Click to See Complete Forum and Search --> : how to find first (and last) bit set in a bitstring?


hotCSchick
August 29th, 2004, 01:44 AM
how do you find the first bit set in a bitstring? (without iterating through the bits of course...)

and how do you find the last bit set?

i think there was a way to do one of the above or both, and i'm trying to figure it out, but can't remember.

thanks for any replies!

Bornish
August 29th, 2004, 04:19 AM
Try this to find the index of the first bit set in x:
n = log(x & (~x+1))/log(2);
For x =0, no error is signaled, but n will be -1.#INF000000000.
Similar, bitwise compute the highest power of 2, in order to find the last bit set.

Bye,