From: Francis Glassborow on
Richard Heathfield wrote:
> Francis Glassborow said:
>
> <snip>
>
>> Well my first thought is to create an array of 256 ints and populate it
>> with the required values:
>>
>> int bitcounts[256] = {0, 1, 2, 1, 2, 2, 3 and so on
>
> UCHAR_MAX + 1, surely?
>
> (One would of course write a program to generate such a table, rather than
> compose it by hand.)
>
>> now I would use that as a lookup table, e.g.
>>
>> long bitcount(int * source, size_t number){
>> long result(0);
>> for(size_t i(0); i != number; ++i) result += bitcounts[source[i]];
>> return result;
>> }
>
> The OP actually said "unsigned char array", so I'm puzzled by int * source.

Just being careless (or put another way, leaving something for other to
think about :-)

>
> I'd have done it like this:
>
> unsigned long bitcount(unsigned char *source, size_t number)
> {
> unsigned long result = 0;
> while(number--)
> {
> result += bitcounts[*source++];
> }
> return result;
> }
>

Yes I hope I would as well if it were for real.