diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/CONTRIB/SNIP/bitcnt_2.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/CONTRIB/SNIP/bitcnt_2.c')
-rwxr-xr-x | reference/C/CONTRIB/SNIP/bitcnt_2.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/SNIP/bitcnt_2.c b/reference/C/CONTRIB/SNIP/bitcnt_2.c new file mode 100755 index 0000000..9046456 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/bitcnt_2.c @@ -0,0 +1,33 @@ +int bitcount(long i)
+{
+ i = ((i & 0xAAAAAAAA) >> 1) + (i & 0x55555555);
+ i = ((i & 0xCCCCCCCC) >> 2) + (i & 0x33333333);
+ i = ((i & 0xF0F0F0F0) >> 4) + (i & 0x0F0F0F0F);
+ i = ((i & 0xFF00FF00) >> 8) + (i & 0x00FF00FF);
+ i = ((i & 0xFFFF0000) >> 16) + (i & 0x0000FFFF);
+ return (int)i;
+}
+
+#ifdef TEST
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define plural_text(n) &"s"[(1 == (n))]
+
+void main(int argc, char *argv[])
+{
+ long n;
+
+ while(--argc)
+ {
+ int i;
+
+ n = atol(*++argv);
+ i = bitcount(n);
+ printf("%ld contains %d bit%s set\n",
+ n, i, plural_text(i));
+ }
+}
+
+#endif /* TEST */
|