lowestBitSet

C

Public Domain

Returns the index of the lowest bit set in an int

Download (right click, save as, rename as appropriate)

Embed

Tags:

bit bits bitwise C set
1
2
3
4
5
int lowestBitSet(unsigned int x) {
 int i=0, j=1;
 for (; !(j & x) && i < sizeof(x) * CHAR_BIT; i++, j <<= 1);
 return j;
}