Simple Recursive Generator Question

David Eppstein eppstein at ics.uci.edu
Fri Dec 19 17:49:10 EST 2003


In article <brvshb$cdp$0 at 216.39.172.122>, bokr at oz.net (Bengt Richter) 
wrote:

> Here is one that works also for negative numbers (includes the least 
> significant
> of the arbitrarily extended sign bits):
> 
>  >>> def bitnos(self):
>  ...     """Little-endian bit number generator"""
>  ...     bits = long(self)
>  ...     sign = bits<0
>  ...     bitno = 0
>  ...     while bits>0 or sign and bits!=-1L:
>  ...         if bits&1: yield bitno
>  ...         bitno += 1
>  ...         bits >>= 1
>  ...     if sign: yield bitno
>  ...

I'm not sure I would call that working -- what I'd expect for a negative 
number is to generate an infinite sequence.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list