summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/bitcnt_2.c
blob: 904645603baf33b885fc717e95583e60e27ba9e1 (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
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 */