headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / string / ffs.cpp
blob7f5ff3f6b3529a12a952a6ed505cb6a93fbcc80d
1 /*
2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <strings.h>
8 // find first (least significant) set bit
9 int
10 ffs(int value)
12 if (!value)
13 return 0;
15 // ToDo: This can certainly be optimized (e.g. by binary search). Or not
16 // unlikely there's a single assembler instruction...
17 for (int i = 1; i <= (int)sizeof(value) * 8; i++, value >>= 1) {
18 if (value & 1)
19 return i;
22 // never gets here
23 return 0;