junky_fellow wrote:
Can anyone suggest some efficient way to find the
number of 1's as well as their position in a 32 bit integer ?
Counting the 1-bits is easy enough. Among the several
approaches are loops with masks and shifts, table-based
methods, "horizontal addition," and hybrids of these. Take
your pick.
As for "their position" -- well, it seems to me the
original 32-bit integer is a pretty efficient expression
of the positions of the 1-bits. What other expression did
you have in mind?
You may also be interested in Sean Eron Anderson's
collection of "Bit Twiddling Hacks"
http://graphics.stanford.edu/~seander/bithacks.html
Be warned that some of the tricks described there are not
entirely portable.
--
Eric Sosman
es*****@acm-dot-org.invalid