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

  1. Newsgroups: comp.arch
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uunet.ca!geac!itcyyz!yrloc!rbe
  3. From: rbe@yrloc.ipsa.reuter.COM (Robert Bernecky)
  4. Subject: Re: Finding which bit set in a bit-string.
  5. Message-ID: <1992Jul30.060923.20663@yrloc.ipsa.reuter.COM>
  6. Keywords: Bit-counting.
  7. Reply-To: rbe@yrloc.ipsa.reuter.COM (Robert Bernecky)
  8. Organization: Snake Island Research Inc, Toronto
  9. References: <1992Jul27.212607.26361@infonode.ingr.com> <1992Jul28.160158.23842@crd.ge.com>
  10. Date: Thu, 30 Jul 92 06:09:23 GMT
  11. Lines: 43
  12.  
  13. In article <1992Jul28.160158.23842@crd.ge.com> davidsen@crd.ge.com (bill davidsen) writes:
  14. >In article <1992Jul27.212607.26361@infonode.ingr.com>, aaronb@suhana.analog.ingr.com (Aaron B.) writes:
  15. >
  16. >| I posted this on "comp.compression":
  17. >| 
  18. >| >Given a bit-string of any size print out the bits set starting with the
  19. >| >least significant bit.
  20.  
  21. I presume you mean the indices of the bits set. Otherwise, you'd end up
  22. with:
  23.    1 1 1 1 1 1  1 1...
  24. >
  25. >  Doing it in C, you don't want a bit by bit test, you want a branch
  26. >table. A byte is too slow with a switch, so use a procedure vector:
  27.  
  28. Well, actually, you shouldn't even have to branch. Index an
  29. appropriate byte-wide (2/4 etc depending on storage available) table,
  30. and print all bits in the byte at once.
  31.  
  32. Of course, in J, you'd do this as:
  33.     X/i. #X
  34.  
  35. That is: #x is the shape of x; ie the number of elements in x.
  36.         i. #x is the integers from 0 through the shape of x.
  37.             That is, if x =. 0 1 1 0 1 1 1 0 1 1 0 0 0 0 1,
  38.             then #x is 15.
  39.         x/y is "replicate", or "copy". That is, make x[i] copies of
  40.              y[i]    
  41.  
  42.  
  43. So, 1 0 2 3 5 / 'abcde' gives: accdddeeeee.
  44.  
  45. By the above, x/i.#x gives the indices of all 1's in x. If there are none,
  46. the result is an empty array, of course.
  47.  
  48.  
  49.  
  50.  
  51. Robert Bernecky      rbe@yrloc.ipsa.reuter.com  bernecky@itrchq.itrc.on.ca 
  52. Snake Island Research Inc  (416) 368-6944   FAX: (416) 360-4694 
  53. 18 Fifth Street, Ward's Island
  54. Toronto, Ontario M5J 2B9 
  55. Canada
  56.