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

Re: Counting bits



	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;
	}

There's a man who has never had to code a critical inner-loop.

When you're exhaustively testing keyspaces, or getting hard crypto
to run at lan speeds, sometimes every cycle is critical.  If the function
above is in the main inner loop (say 80% of the CPU time as gleaned
from a profile utility), the optimisations people suggested will
speed your program up by a factor of 10.  This is the one time
that bit-twiddling optimisations are worthwhile.  (Mostly they're
irrelevant and just posturing by smart-ass kiddies...)

G