🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.compression.research
Article #38

Algorithm for finding set bits in a bit-string.

#38
From: Aaron B.
Date: Sun, 26 Jul 1992 01:03
48 lines
1491 bytes
I don't know where this request should belong, this is the closest group
I can find.  Anyway, this is my problem (my friend's actually):

Given a bit-string of any size print out the bits set starting with the
least significant bit.  This is an algorithm that I offered him.  But I
think there is much better way of doing it.


static unsigned char binvalue[9]={0x00, 0x01, 0x02, 0x04, 0x08,
                                        0x10, 0x20, 0x40, 0x80 };

int
show_msb(ch, off)
unsigned char ch;
long          off;
{
  int pos;

  if ( ch )
   {
     pos = ilog2( ch );
     printf("position = %d\n",pos+off);
     ch ^= binvalue[pos];         /* could also use: ch -= binvalue[pos]; */
     show_msb(ch, off);           /* recursive call for rest of bits */
   }
  return( 0 );
}

int
ilog2(n)
register unsigned char n;
{
   register long q;

   q = 0;
   while( n )
    {
      n >>= 1;
      q++;
    }
   return( q );
}   /* ilog2 */


Thanks in advance.

Aaron B
aaronb@suhana.analog.ingr.com
Technical Details
Message-ID: <1992Jul26.010325.9885@infonode.ingr.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!infonode!ingr!analog!suhana!aaronb

Show Full Headers
Complete Article Headers
Newsgroups: comp.compression.research
Path: arc1-ca.pugleaf.net!sparky!uunet!infonode!ingr!analog!suhana!aaronb
From: aaronb@suhana.analog.ingr.com (Aaron B.)
Subject: Algorithm for finding set bits in a bit-string.
Message-ID: <1992Jul26.010325.9885@infonode.ingr.com>
Keywords: Bit counting.
Sender: harun@suhana (Aaron B.)
Reply-To: aaronb@suhana.analog.ingr.com
Organization: Home Office Enterprise
Date: Sun, 26 Jul 1992 01:03:25 GMT
Lines: 48