Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!usc!srhqla!demott!kdq From: kdq@demott.COM (Kevin D. Quitt) Newsgroups: comp.lang.c Subject: Re: count of bits set in a long Message-ID: <613@demott.COM> Date: 27 Sep 90 16:56:14 GMT References: Reply-To: kdq@demott.COM (Kevin D. Quitt) Organization: DeMott Electronics Co., Van Nuys CA Lines: 29 In article sa1z+@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@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.