Added ffs().

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11614 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2005-03-07 22:02:50 +00:00
parent d29c4f5287
commit 5141596a69
2 changed files with 26 additions and 0 deletions
+2
View File
@@ -77,6 +77,8 @@ extern const char *strsignal(int signal);
#define bcopy(source, dest, length) memcpy((dest), (source), (length))
#define bzero(buffer, length) memset((buffer), 0, (length))
extern int ffs(int i);
#ifdef __cplusplus
}
#endif
+24
View File
@@ -0,0 +1,24 @@
/*
* Copyright 2005, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include <strings.h>
// find first (least significant) set bit
int
ffs(int value)
{
if (!value)
return 0;
// ToDo: This can certainly be optimized (e.g. by binary search). Or not
// unlikely there's a single assembler instruction...
for (int i = 1; i <= (int)sizeof(value) * 8; i++, value >>= 1) {
if (value & 1)
return i;
}
// never gets here
return 0;
}