recursion depth problem

proctor 12cc104 at gmail.com
Sun Apr 22 22:13:31 EDT 2007


On Apr 22, 7:10 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On 22 Apr 2007 17:06:18 -0700, proctor <12cc... at gmail.com> declaimed the
> following in comp.lang.python:
>
> > >     else:
> > >         # only one of carry in, b1, or b2 is set
>
>                         #or none is set! Missed the 0,0,0 condition <G>
>
> > >         return (0, b1 + b2 + c)
>
> > thank you.  you guys are keeping me busy!
>
>         Heh... I'm sure what I scratched together could be optimized more
> (make functions out of the input/output conversions; add some error
> checking on input data, allow for non-list arguments in adder()...)
>
>         After all, if one wants a binary counter, one should implement a
> binary addition operation, and basic digital has ways... Unfortunately,
> Python now has a Boolean type, so boolean AND/OR doesn't give the
> desired results... And using bitwise seems wasteful <G> However...
> replace the badd() with the following obscure thing if you really want
> to get close to hardware emulation...
>
> def badd(b1, b2, ci=0):
>     """
>         badd(b1, b2, ci) => (co, sum)
>     """
>     (co1, hs1) = (b1 & b2, b1 ^ b2)
>     (co2, hs2) = (ci & hs1, ci ^ hs1)
>     return (co1 | co2, hs2)
>
>         A pair of "half-adders"; and the extra bit to make a "full-adder".
> It wouldn't be efficient to do it as one long return, just due to
> duplicated (b1 ^ b2) sub-terms
>
> return ( (b1 & b2) | (ci & (b1 ^ b2)), (ci ^ (b1 ^ b2)))
>
> but it does work <G>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/

:-)

this is good stuff.  for learning especially!  thank you again!

proctor.




More information about the Python-list mailing list