Message-Id: <37B53B6E.7CF29EB1@intel.com> Date: Sat, 14 Aug 1999 11:48:30 +0200 From: Kurt Alstrup Organization: Intel Denmark Aps X-Mailer: Mozilla 4.51 [en] (WinNT; U) X-Accept-Language: en Mime-Version: 1.0 To: djgpp AT delorie DOT com Subject: Re: Bit counting? References: <37B45836 DOT EAD7C82D AT swipnet DOT se> <37B48ABE DOT 110D2C13 AT intel DOT com> <37B4D8BC DOT 6465 AT ns DOT sympatico DOT ca> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com Well ... Looking at it again then a modulo 077 (= 0x3f) could be accompliced by using '&' instead. Thus the line return (int) (((y + (y >> 3)) & 030707070707) & 077); should do the same without using a potential slow mod operator. Kurt Alstrup Klaas wrote: > Kurt Alstrup wrote: > > > > Try this little function, I guess it may gain if written in assembly > > though .. > > > > /* > > * Ones > > * > > * This magic counts the number of bits in one longword > > */ > > > > int > > Ones(unsigned long mask) > > { > > register unsigned long y; > > > > y = (mask >> 1) & 033333333333; > > y = mask - y - ((y >>1) & 033333333333); > > return (int) (((y + (y >> 3)) & 030707070707) % 077); > > } > > > > Regards, > > Kurt Alstrup > > Doesn't the modulo make it rather slow? > > -Mike