iterating bit-by-bit across int?

Diez B. Roggisch deets_noospaam at web.de
Thu Oct 23 15:32:29 EDT 2003


> I'm thinking about writing a function that eats integers and poops out
> lists of bools; and then I can iterate through that, and change values
> in there.  But before I do that, does anyone have a better idea?

For speed, you should use shift and boolean ops - like this:

def mutate(seq, n=32, prob=0.05):
  for bit in xrange(n):
    if random.random() <= prob:
      seq ^= 1 << bit
  return seq

Regards,

Diez




More information about the Python-list mailing list