Path: utzoo!attcan!uunet!know!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!decuac!shlump.nac.dec.com!star.enet.dec.com!parke From: parke@star.enet.dec.com (Bill Parke) Newsgroups: comp.lang.c Subject: Re: count of bits set in a long Message-ID: <1990Sep26.163754@star.enet.dec.com> Date: 26 Sep 90 20:39:23 GMT References: <34281@cup.portal.com> <37545@ut-emx.uucp> <3820@goanna.cs.rmit.oz.au> Sender: newsdaemon@shlump.nac.dec.com Reply-To: parke@star.enet.dec.com (Bill Parke) Organization: Digital Equipment Corporation, Nashua NH Lines: 30 In article <34281@cup.portal.com>, ts@cup.portal.com (Tim W Smith) writes: |> From: ts@cup.portal.com (Tim W Smith) |> Newsgroups: comp.lang.c |> Subject: Re: count of bits set in a long |> |> How about something like this? |> |> int NumberOfBitsInLong( register unsigned long input ) ^^^^^^^^ Not necessary |> { |> register int count; |> |> for ( count = 0; input != 0; count++ ) |> input &= input-1; This will count bits in a signed number (see my previous posting of this using "while" ) |> |> return count; |> } |> |> Tim Smith |> -- Bill Parke parke%star.enet.dec@decwrl.dec.com VMS Development decwrl!star.enet.dec.com!parke Digital Equipment Corp parke@star.enet.dec.com 110 Spit Brook Road ZK01-1/F22, Nashua NH 03063 The views expressed are my own.