Add two binary numbers given as two strings.
Utilisateur anonyme
In Python: s1 = raw_input() s2 = raw_input() carry = 0 out = "" for i in range(0, max(len(s1), len(s2))): pos1 = len(s1) - i - 1 pos2 = len(s2) - i - 1 v1 = int(s1[pos1]) if pos1 >= 0 else 0 v2 = int(s2[pos2]) if pos2 >= 0 else 0 out = str((v1 + v2 + carry) % 2) + out carry = (v1 + v2 + carry) / 2 if carry == 1: out = "1" + out print out --- If you are allowed to convert strings to integers, the solution would be even simpler to interpret: s1 = int(raw_input()) s2 = int(raw_input()) carry = 0 out = "" while s1 > 0 or s2 > 0: v1 = s1 % 10 v2 = s2 % 10 s1 /= 10 s2 /= 10 out = str((v1 + v2 + carry) % 2) + out carry = (v1 + v2 + carry) / 2 if carry == 1: out = "1" + out print out