int main()
{
int x = 128; // change the number for validate
int bit_pos = 0;
bit_pos = ((( x & 0xFFFF0000 ) != 0) << 4 ) +
((( x & 0xFF00FF00 ) != 0) << 3 ) +
((( x & 0xF0F0F0F0 ) != 0) << 2 ) +
((( x & 0xCCCCCCCC ) != 0) << 1) +
(( x & 0xAAAAAAAA ) != 0);
printf("bit_possition which is on : %d\n", bit_pos);
return 0;
}
2 : Or you can use ffs() "find first bit set in a word" (it counts from 1 not from 0)
I didn't sure that this function use or didn't use loop internally.
int main()
{
int num = 32;
printf("bit_postion : %d\n", fss(num));
return 0;
}