BIT Counting: C programming problem

Dave Jones djones at megatest.UUCP
Wed Jan 11 09:44:47 AEST 1989


>From article <34389 at bbn.COM), by cosell at bbn.com (Bernie Cosell):
) In article <225 at tityus.UUCP) jim at athsys.uucp (Jim Becker) writes:
) }
) }	The problem is the most efficient method to simply count the
) }number of on bits in a sixteen bit short. 
) 
) 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."



More information about the Comp.lang.c mailing list