www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/13/14:53:47

From: Groman <groman AT tfz DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Bit counting?
Date: Fri, 13 Aug 1999 11:15:57 -0700
Organization: Posted via RemarQ, http://www.remarQ.com - The Internet's Discussion Network
Lines: 36
Message-ID: <37B460DD.419D0700@tfz.net>
References: <37B45836 DOT EAD7C82D AT swipnet DOT se>
Mime-Version: 1.0
X-Trace: 934568136 WQ39EUJ1V392DB781 usenet44.supernews.com
X-Complaints-To: newsabuse AT remarQ DOT com
X-Mailer: Mozilla 4.04 [en] (WinNT; U)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

what would I do, is the following:
I would create a byte with the value of 1(binary 00000001)
and take your byte and do a bitwise "and"
char a=1;
char b=YOUR_NUMBER;
int i=0;
int z;
for(z=0;z<8;z++)
{
if((a | b) = 1)
    i++;
a << 1;  //I haven't programmed in C for 4 years now, so I guess that's
how is the bit shift, but I am not sure
}

you check the bit, you shift, check shift, etc.
but I am not sure if the code is correct, or algorithm is the fastest,
but hope this helps.

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)
>
> Thanks!
>
> David Skarin



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019