diff options
Diffstat (limited to 'test/benchmarks/bit_count1.c')
-rwxr-xr-x | test/benchmarks/bit_count1.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/test/benchmarks/bit_count1.c b/test/benchmarks/bit_count1.c new file mode 100755 index 0000000..c900524 --- /dev/null +++ b/test/benchmarks/bit_count1.c @@ -0,0 +1,30 @@ +/* + * Bit counter by Ratko Tomic + */ + +long atol(char *); +int printf(char *s, ...); + +int bit_count(long x) +{ + int n = 0; + if (x) do + n++; + while (0 != (x = x&(x-1))) ; + return(n); +} + +int main(int argc, char **argv) +{ + long n = atol(*++argv); + long i; + int sum = 0; + + if (n == 0) + return 1; + + for (i = 0; i < n; ++i) + sum += bit_count(i); + printf("%d\n", sum); + return 0; +} |