Message-Id: <37B48ABE.110D2C13@intel.com> Date: Fri, 13 Aug 1999 23:14:38 +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> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Reply-To: djgpp AT delorie DOT com 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 PS. This is the 2nd reply, the 1st one came back with the note ----- The following addresses had delivery problems ----- (unrecoverable error) ----- Transcript of session follows ----- ... while talking to delorie.com.: >>> DATA <<< 500 Hey, offensive language ("d|a|m|n") is not allowed. 554 ... Remote protocol error ----- Original message follows ----- As I just replied to mail message I originally got from the djgpp mailing list I fail to understand why the original mail got through in the first place. Secondly I disagree that it was offensive languange, propably strong, but offensive; definitely no. Anders David Skarin wrote: > Hi, > > What is the fastest way (or just a <> fast way) to > count the nr of binary 1's in a byte/word/dword ? > > Is there any assembler instruction for this ? > (there should be, considering the nr of instructions > in the sisc processors) > > Thanks! > > David Skarin