Wrong code analysis? Or my code is "strange"?

Hi There! :slight_smile:

First sorry for my poor English!

I noticed that if I send this input into Playground (with the settings taken from the examples):


#Python 3
def sommabit(a, b, riportoprecedente, posizione):
if posizione == 0:
s = (not (a) and b) or (a and not (b))
r = a and b
return s, r, 0
elif posizione == 1:
s = ((not (a)) and (not (b)) and riportoprecedente) or ((not (a)) and (b) and (not (riportoprecedente))) or (a and (not (b))and (not (riportoprecedente))) or (a and b and riportoprecedente)
r = (b and riportoprecedente) or (a and riportoprecedente) or (a and b)
return s, r, riportoprecedente
else:
s, r, riportoprecedente = sommabit(a, b, riportoprecedente, (posizione - 1))
return s, r, riportoprecedente

#Explanation of what the code does


The output is:


#The function sommabit takes 3 arguments:
#a and b are the two bits to be added
#riportoprecedente is the carry from the previous addition
#posizione is the position of the bit to be added
[…]


So the sommabit function has an input of 4 parameters but only 3 are recognized?

The sommabit function tries to simulate a CLA (Carry Look Ahead) (and is recursive, as you can see).

The full code for the simulation is:


#addizionatore a 4 bit per 2 numeri senza overflow
#funzione sommabit: effettua somme di bit ricorsivamente. Vuole in input: il primo numero da sommare, il secondo numero da sommare, il riporto al passo precedente e la posizione a cui stiamo sommando
def sommabit(a, b, riportoprecedente, posizione):
if posizione == 0:
s = (not (a) and b) or (a and not (b))
r = a and b
return s, r, 0
elif posizione == 1:
s = ((not (a)) and (not (b)) and riportoprecedente) or ((not (a)) and (b) and (not (riportoprecedente))) or (a and (not (b))and (not (riportoprecedente))) or (a and b and riportoprecedente)
r = (b and riportoprecedente) or (a and riportoprecedente) or (a and b)
return s, r, riportoprecedente
else:
s, r, riportoprecedente = sommabit(a, b, riportoprecedente, (posizione - 1))
return s, r, riportoprecedente
PrimoNumeroDaSommare = int(input("Inserisci il primo numero da sommare: "))
SecondoNumeroDaSommare = int(input("Inserisci il secondo numero da sommare: "))
#trasformo in stringhe l’input
PrimoNumeroDaSommare_Stringa = str(bin(PrimoNumeroDaSommare))
SecondoNumeroDaSommare_Stringa = str(bin(SecondoNumeroDaSommare))
#sistema la lunghezza delle stringhe di input
if PrimoNumeroDaSommare < 2:
PrimoNumeroDelPrimoNumeroDaSommare = 0
SecondoNumeroDelPrimoNumeroDaSommare = 0
TerzoNumeroDelPrimoNumeroDaSommare = 0
QuartoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[2])
elif 2 <= PrimoNumeroDaSommare < 4:
PrimoNumeroDelPrimoNumeroDaSommare = 0
SecondoNumeroDelPrimoNumeroDaSommare = 0
TerzoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[2])
QuartoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[3])
elif 4 <= PrimoNumeroDaSommare < 8:
PrimoNumeroDelPrimoNumeroDaSommare = 0
SecondoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[2])
TerzoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[3])
QuartoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[4])
else:
PrimoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[2])
SecondoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[3])
TerzoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[4])
QuartoNumeroDelPrimoNumeroDaSommare = int(PrimoNumeroDaSommare_Stringa[5])
if SecondoNumeroDaSommare < 2:
PrimoNumeroDelSecondoNumeroDaSommare = 0
SecondoNumeroDelSecondoNumeroDaSommare = 0
TerzoNumeroDelSecondoNumeroDaSommare = 0
QuartoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[2])
elif 2 <= SecondoNumeroDaSommare < 4:
PrimoNumeroDelSecondoNumeroDaSommare = 0
SecondoNumeroDelSecondoNumeroDaSommare = 0
TerzoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[2])
QuartoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[3])
elif 4 <= SecondoNumeroDaSommare < 8:
PrimoNumeroDelSecondoNumeroDaSommare = 0
SecondoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[2])
TerzoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[3])
QuartoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[4])
else:
PrimoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[2])
SecondoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[3])
TerzoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[4])
QuartoNumeroDelSecondoNumeroDaSommare = int(SecondoNumeroDaSommare_Stringa[5])
PrimoNumeroLista = [QuartoNumeroDelPrimoNumeroDaSommare, TerzoNumeroDelPrimoNumeroDaSommare, SecondoNumeroDelPrimoNumeroDaSommare, PrimoNumeroDelPrimoNumeroDaSommare]
SecondoNumeroLista = [QuartoNumeroDelSecondoNumeroDaSommare, TerzoNumeroDelSecondoNumeroDaSommare, SecondoNumeroDelSecondoNumeroDaSommare, PrimoNumeroDelSecondoNumeroDaSommare]
Risultato = [0, 0, 0, 0]
RiportoPrecedente = 0
PostoDaSommare = 0
Riporto = 0
while PostoDaSommare < 4:
Risultato[PostoDaSommare], Riporto, RiportoPrecedente = sommabit(PrimoNumeroLista[PostoDaSommare], SecondoNumeroLista[PostoDaSommare], RiportoPrecedente, PostoDaSommare)
PostoDaSommare = PostoDaSommare + 1
RiportoPrecedente = Riporto
Valore = 0
for Valore in Risultato:
print int(Valore)


Sum only up to 4 bits (which means that both the input numbers and the result must be between 0 and 15).

The code and article explaining the recursive mathematical property of CLA and its processing are at https://www.ingegnerealbano.com/somma-ricorsiva-in-python/

The article is in Italian.

Thx in advance for your patience :slight_smile: