I have a uint64_t
and I would like to find the index of the first set bit, reset it to zero and find the next set bit.
How do I know when to terminate? BSF
2 points to keep in mind when using __builtin_ffs
:
while (m) {
// Get the rightmost bit location.
int BitOffset = __builtin_ffs(m);
// Clear the bit before the next iteration.
// Used in the loop condition.
m = (m >> BitOffset) << BitOffset;
// Do your stuff .....
}