[courses] [C] lesson 10 exercise 1 ans (sorry for the # of posts)
Lorne Gutz
lgutz at vistar.ca
Wed Nov 13 11:33:05 EST 2002
>
> for(i = 0; i < BITS; i++) {
> if( ((input & (1 << i)) >> i)) count++;
> /* that was the part I don't understand */
The if statment seems to have a lot of extra baggage.
It should be simplified to read:
if( ( input & ( 1 << i )) )
count++;
Not sure what the >> i was added for but it is NOT
required.
Let me quote the C standard:
In all cases, if the control expression is 0, it is taken to be "false";
if it is non-zero, it is taken to be 'true'. More precisely, the type of a
control expression e must be such that the expression
( e ) != 0
may be legally evaluated. If the result of this last expression is 1, e
is said to be non-zero; otherwise, e is said to be zero. In practice,
this means that e may have integral, pointer, or floating-point type.
Values of enumeration types may also be permitted depending on the
semantics chosen for enumerations.
KISS :)
cheers
Lorne
More information about the Courses
mailing list