BIT Counting: C programming problem

Alan J Rosenthal flaps at dgp.toronto.edu
Mon Jan 16 12:27:34 AEST 1989


There seems to be debate over whether or not the algorithm posted works, and
there was even a ``proof'' that it does work, which is very interesting.

It doesn't work for MININT unless (MININT - 1) happens to evaluate to MAXINT
(as it frequently does in real life).  On a one's complement machine, I doubt
that MININT - 1 == MAXINT very often; it's probably -0 (if it's not trapped).

If MININT - 1 is an overflow, the posted algorithm certainly won't give the
right answer for MININT (the right answer being 1 on a two's complement
machine, or log2(MAXINT + 1) on a one's complement machine).

ajr




More information about the Comp.lang.c mailing list