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