Ok, Abkürzungen gelten nicht:nomnom hat geschrieben:@jerch: Du benutzt doch in deinem Code ständig "-", das ist doch unfair :p Dann geht auchCode: Alles auswählen
add = lambda a, b: a - (-b)
Code: Alles auswählen
ZERO = 0
ONE = 1
# simple 1 testing
def is_one(a):
return ONE if a==ONE else ZERO
# xor equal testing
def equal(a, b):
return is_one(((a^b) << ONE) | ONE)
# testing for zero (lazy version with using equal)
def is_zero(a):
return equal(a, ZERO)
# simple incrementor
def inc(a):
if equal(a, ~ZERO):
return ZERO
b = ONE
while ONE:
if is_zero(b):
return a
a, b = a^b, (a&b) << ONE
# negativate number: positive to negative and negative to positive
def neg(a):
if is_zero(a):
return a
return inc(~a)
(Zur Anschaulichkeit hab ich 1 und 0 mal durch "Konstanten" ersetzt)