count of bits set in a long

Kevin D. Quitt kdq at demott.COM
Fri Sep 28 02:56:14 AEST 1990


In article <Mazyneu00WB6EAjWo8 at andrew.cmu.edu> sa1z+ at andrew.cmu.edu (Sudheer Apte) writes:
>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? 
>

    Massively.  Look at the code produced - it is generally two or three
instructions per byte. 

-- 
 _
Kevin D. Quitt         demott!kdq   kdq at demott.com
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  MODEM (818) 997-4496 PEP last

                96.37% of all statistics are made up.



More information about the Comp.lang.c mailing list