home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.arch
- Path: sparky!uunet!usc!sol.ctr.columbia.edu!ira.uka.de!smurf.sub.org!incom!orfeo!darkcube!vhs
- From: vhs@darkcube.radig.de (Volker Herminghaus-Shirai)
- Subject: Re: Algorithm for finding set bits in a bit-string (and MSB).
- Message-ID: <1992Jul30.115700.19573@darkcube.radig.de>
- Sender: vhs@darkcube.radig.de (Volker Herminghaus-Shirai)
- Reply-To: vhs@darkcube.radig.de
- References: <Bs46JC.35n@watcgl.uwaterloo.ca>
- Date: Thu, 30 Jul 92 11:57:00 GMT
- Lines: 40
-
- In many cases it is fastest to just use a lookup table for counting,
- outputting, or whatever-ing bits in a byte. With proper alignment
- handling and incrementation this can easily be generalized to bit-strings
- of arbitrary length. So in the case you want to output a string showing
- an ASCII representation of the bits in a bit string (as the original poster
- did) you define:
-
- /*
- * I didn't test this code. It also may need rewriting if
- * different direction is needed (backwards). In this case, you might use
- * sprintf or equivalent instead of printf and print the string backwards
- */
- char *strings[]={
- "00000001",
- "00000010",
- "00000011",
- "00000100",
- ...
- };
-
- printString(char *bitString, length)
- {
- /* BOS probably best handled by pre-aligning bitString such */
- /* that it always starts on a byte border */
- while((length-=8) > 0) {
- /* proper EOS-handling (e.g. by &ing *bitString and */
- /* truncating strings[*bitString] */
- printf(strings[*bitString++]);
- }
- }
-
- As I said, the code above is untested. It just went from the fingers to the
- keyboard without ever touching my brain ;-). The end condition of the while
- loop probably needs reworking, but I don't feel quite well with >>39
- Centigrades. Guess I shouldn't have eaten that darn fish :-(.
-
- --
- Volker Herminghaus-Shirai (vhs@darkcube.radig.de)
-
- Cause I have fish inside(tm), I'm...
-