From: "Campbell, Rolf [SKY:1U32:EXCH]" Newsgroups: comp.os.msdos.djgpp Subject: Re: Bit counting? Date: Fri, 13 Aug 1999 14:50:28 -0400 Organization: Nortel Networks Message-ID: <37B468F3.8D06584A@americasm01.nt.com> References: <37B45836 DOT EAD7C82D AT swipnet DOT se> NNTP-Posting-Host: bmerhc00.ca.nortel.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (X11; I; HP-UX B.10.20 9000/712) X-Accept-Language: en Lines: 27 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Anders David Skarin wrote: > Hi, > > What is the fastest way (or just a damn 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) There are instructions for bsf (bit scan forward), but I don't think for bitcount. Here some good C code for it. [Untested!] int count=0; int data=whatever; while(data&=(data-1)) count++; - -Rolf Campbell (39)3-6318