home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / arch / 8473 < prev    next >
Encoding:
Text File  |  1992-07-30  |  1.8 KB  |  52 lines

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