while True or while 1

Ian ian.g.kelly at gmail.com
Thu Dec 16 19:00:39 EST 2010


On Dec 16, 4:34 pm, "BartC" <b... at freeuk.com> wrote:
> def p2(n):
>   p=1
>  whileTrue:
>     if n<=p: return p
>     p<<=1
>   return 0
>
> for i in xrange(1000000):
>   x=p2(i)
>
> p2() calculates the smallest power of 2 >= it's operand.

def p2(n):
  return 1 << n.bit_length()



More information about the Python-list mailing list