home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!ucbvax!ucdavis!toadflax!lim
- From: lim@toadflax.cs.ucdavis.edu (Lloyd Lim)
- Newsgroups: comp.sys.mac.programmer
- Subject: Re: (Q) Need bit manipulation algorithm
- Summary: bit counting and gratuitous Microsoft bashing
- Message-ID: <20751@ucdavis.ucdavis.edu>
- Date: 6 Jan 93 10:19:11 GMT
- References: <1993Jan6.022430.19469@leland.Stanford.EDU> <1993Jan6.032113.597@planets.risc.rockwell.com>
- Sender: usenet@ucdavis.ucdavis.edu
- Organization: Department of Computer Science, University of California, Davis
- Lines: 26
-
- In article <1993Jan6.032113.597@planets.risc.rockwell.com> sho@risc.rockwell.com (Sho Kuwamoto) writes:
- >
- >Maybe not the fastest method, but a simple one:
- > 1) come up with an algorithm which counts the number of
- > 1's in a given bit pattern. You'll either use a loop
- > with shifts and increments in it, or you'll use masks.
- >[...]
-
- There is a neat trick for counting the number of bits in a word that
- a Microsoft geek showed me one time. If there are k bits with 1's,
- it runs in O(k) time.
-
- I don't remember the algorithm, but it involved subtracting 1 and could
- be written in one line of C. (Yeah, I know... You could write anything,
- even Microsoft Word, in one very long line of C code. Come to think of it,
- that's probably what they did.) The subtraction was used to create
- instant masks: e.g. 0x0100 - 1 = 0x00FF. It went from the highest 1 bit
- to the lowest, magically avoiding all of the 0's.
-
- That's all I remember. Anyone remember this algorithm?
-
- +++
- Lloyd Lim Internet: lim@cs.ucdavis.edu
- Lim Unlimited America Online: LimUnltd
- 330 W. Iris Ave. AppleLink: LimUnltd
- Stockton, CA 95210-3738 CompuServe: 72647,660
-