On 21 Feb 2005 06:48:19 -0500, rumours say that Dan Sommers <[EMAIL PROTECTED]>
might have written:

[snip: snacktime posts code to count bits]

>> Seems to work, is there a better way to do this?

[Dan]
>for c in range( 128 ):
>    even_odd = 0
>    print '%3d' % c,
>    while c:
>        c &= c - 1
>        even_odd = not even_odd
>    print int( even_odd )

Just for the sake of people who haven't messed with bit manipulation in C or
assembly, the effect of

c &= c - 1

is to reset the rightmost (less significant) '1' bit of a number (ie change it
to '0').
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to