On Apr 22, 7:10 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On 22 Apr 2007 17:06:18 -0700, proctor <[EMAIL PROTECTED]> 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 > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/
:-) this is good stuff. for learning especially! thank you again! proctor. -- http://mail.python.org/mailman/listinfo/python-list