Mack wrote:

> Hi all,

> I want to write a program to count number of bits set in a number.

> The condition is we should not loop through each bit to find whether

> its set or not.
Start by writing down a few numbers along with

their bit counts, using any method you like to count

the bits:

Number Bits

0 0

1 1

2 1

3 2

Now find the straight line that gives the best

least-squares fit to the observed data (any spreadsheet

will do this for you if you can't recall the math).

Take the fitted coefficients and write your function:

double bit_count(int number) {

return 0.6 * number + 0.1;

}

Use more data points, if you like, to get a statistically

more accurate fit. There, that wasn't so hard, was it?

--

Eric Sosman

http://www.velocityreviews.com/forums/(E-Mail Removed)lid