summaryrefslogtreecommitdiff
path: root/test/benchmarks/bit_count2.c
blob: a57c1234b5594e0753535d276bfbe40a2cc5ece1 (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
31
32
33
34
35
36
/* +++Date last modified: 05-Jul-1997 */

int bitcount(long i)
{
      i = ((i & 0xAAAAAAAAL) >>  1) + (i & 0x55555555L);
      i = ((i & 0xCCCCCCCCL) >>  2) + (i & 0x33333333L);
      i = ((i & 0xF0F0F0F0L) >>  4) + (i & 0x0F0F0F0FL);
      i = ((i & 0xFF00FF00L) >>  8) + (i & 0x00FF00FFL);
      i = ((i & 0xFFFF0000L) >> 16) + (i & 0x0000FFFFL);
      return (int)i;
}

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;
      for (i = 0; i < n; ++i)
	      sum += bit_count(i);
      printf("%d\n", sum);
      return 0;
}