[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bit counting



>Why bother when you can simply do an eight line function?

>int bitcount(char b)
>{
>register int retval=0;

> if (a & 1) retval++;
> if (a & 2) retval++;
> if (a & 4) retval++;
> if (a & 8) retval++;
> if (a & 16) retval++;
> if (a & 32) retval++;
> if (a & 64) retval++;
> if (a & 128) retval++; 

>return retval;
>}

[...]

Because on a lot of architectures this implementation may be hideously
inefficient.  All the world is not an Intel chip, thank god.

							Ian.