count of bits set in a long
Sudheer Apte
sa1z+ at andrew.cmu.edu
Wed Sep 26 09:48:58 AEST 1990
> From article <37545 at ut-emx.uucp>, by nwagner at ut-emx.uucp (Neal R. Wagner):
> >
> > I need a fast method to count the number of bits that are set in a 32-bit
> > integer on a Sun 3/80 running 4.0.3 SunOS. ...
jc at atcmp.nl (Jan Christiaan van Winkel) writes:
> [... a really neat iteration, one per bit at most, deleted ]
That was probably what Karl Heuer was talking about leaving for others.
I was thinking along these lines:
static int num_bits[256] = {0, 1, 1, 2, 1, ... /* etc. */ };
struct four_pack { char a; char b; char c; char d;};
int bit_count(i)
unsigned long i;
{
struct four_pack f;
f = (struct four_pack) i;
return num_bits[f.a] + num_bits[f.b] + num_bits[f.c] +
num_bits[f.d];
}
Would this be faster, in reality, than shifts and iteration?
Sudheer.
------------------
...{harvard, uunet}!andrew.cmu.edu!sa1z
sa1z%andrew at CMCCVB.BITNET
More information about the Comp.lang.c
mailing list