BIT Counting: C programming problem

greg yachuk greggy at infmx.UUCP
Thu Jan 12 11:14:15 AEST 1989


In article <1171 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
:From article <34389 at bbn.COM), by cosell at bbn.com (Bernie Cosell):
:) 
:) Try this:
:) 
:) int BitCount(num)
:)     short num;
:) {
:)     int count = 0;
:) 
:)     while (num != 0)
:) 	num &= num-1, count += 1;
:)     return (count);
:) }
:) 
:
:Note well that he did not claim that it works. He only said, "Try this."

Guess what, Dave.  It *does* work.  Check it out manually, if necessary!

Greg Yachuk	Informix Software Inc., Menlo Park, CA	(415) 322-4100
{uunet,pyramid}!infmx!greggy	why yes, I DID choose that login myself



More information about the Comp.lang.c mailing list