Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ffs.c
Go to the documentation of this file.
1 /* ffs.c - find first set bit */
2 /* ffs() is defined by Single Unix Specification. */
3
4 #include "ruby.h"
5
6 int ffs(int arg)
7 {
8 unsigned int x = (unsigned int)arg;
9 int r;
10
11 if (x == 0)
12 return 0;
13
14 r = 1;
15
16 #if 32 < SIZEOF_INT * CHAR_BIT
17 if ((x & 0xffffffff) == 0) {
18 x >>= 32;
19 r += 32;
20 }
21 #endif
22
23 if ((x & 0xffff) == 0) {
24 x >>= 16;
25 r += 16;
26 }
27
28 if ((x & 0xff) == 0) {
29 x >>= 8;
30 r += 8;
31 }
32
33 if ((x & 0xf) == 0) {
34 x >>= 4;
35 r += 4;
36 }
37
38 if ((x & 0x3) == 0) {
39 x >>= 2;
40 r += 2;
41 }
42
43 if ((x & 0x1) == 0) {
44 x >>= 1;
45 r += 1;
46 }
47
48 return r;
49 }
50