1
0
mirror of https://github.com/ioacademy-jikim/debugging synced 2025-06-07 07:56:19 +00:00
2015-12-13 22:34:58 +09:00

43 lines
698 B
C

#include <stdio.h>
#define BITS_PER_LONG 32
unsigned long __fls(unsigned long word)
{
int num = BITS_PER_LONG - 1;
#if BITS_PER_LONG == 64
if (!(word & (~0ul << 32))) {
num -= 32;
word <<= 32;
}
#endif
if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
num -= 16;
word <<= 16;
}
if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
num -= 8;
word <<= 8;
}
if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
num -= 4;
word <<= 4;
}
if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
num -= 2;
word <<= 2;
}
if (!(word & (~0ul << (BITS_PER_LONG-1))))
num -= 1;
return num;
}
int main()
{
unsigned long item=0;
item |= 1<<23;
item |= 1<<11;
printf("%lu\n", __fls(item));
return 0;
}