Line | |
---|
1 | #include <strings.h> |
---|
2 | |
---|
3 | int ffs(int i) { |
---|
4 | int plus=0; |
---|
5 | /* return index of rightmost bit set */ |
---|
6 | /* ffs(1) == 1, ffs(2) == 2, ffs(256) == 9, ffs(257)=1 */ |
---|
7 | #if 0 |
---|
8 | if (sizeof(i)==8) /* fold 64-bit archs */ |
---|
9 | if ((i&0xffffffff)==0) { |
---|
10 | plus=32; |
---|
11 | i>>=32; |
---|
12 | } |
---|
13 | #endif |
---|
14 | if ((i&0xffff)==0) { |
---|
15 | plus+=16; |
---|
16 | i>>=16; |
---|
17 | } |
---|
18 | if ((i&0xff)==0) { |
---|
19 | plus+=8; |
---|
20 | i>>=8; |
---|
21 | } |
---|
22 | if ((i&0xf)==0) { |
---|
23 | plus+=4; |
---|
24 | i>>=4; |
---|
25 | } |
---|
26 | if (i&1) return plus+1; |
---|
27 | if (i&2) return plus+2; |
---|
28 | if (i&4) return plus+3; |
---|
29 | if (i&8) return plus+4; |
---|
30 | return 0; |
---|
31 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.