summaryrefslogtreecommitdiff
path: root/test/benchmarks/bit_count1.c
blob: c90052407508e81150241b7240abf6ba2d42341f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}