python2.4 generator expression > python2.3 list expression

Terry Reedy tjreedy at udel.edu
Mon Feb 21 17:07:57 EST 2005


"Christos TZOTZIOY Georgiou" <tzot at sil-tec.gr> wrote in message 
news:0ioj11dsgqhatqc3d7ldeb35rta6d9fk69 at 4ax.com...
> On 21 Feb 2005 06:48:19 -0500, rumours say that Dan Sommers 
> <me at privacy.net>
>>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').

Cute.  I tried it a few times until I saw why it works.  But it is also 
dangerous (within a loop like the above) in a language like current Python 
(and unlike C/assembler) in which the binary representation of -1 is 
effectively a left infinite string of '1's: ...1111111111

Terry J. Reedy






More information about the Python-list mailing list