### 67.二進制求和

``````        buff = 0
s = ''
flag = 1
flag1 = 1
l = min(len(a), len(b))
if len(a) > len(b):
temp = a[len(a) - l:]
min_string = b
other = a[:len(a) - l]
elif len(a) < len(b):
temp = b[len(b) - l:]
min_string = a
other = b[:len(b) - l]
else:
flag1 = 0
temp = a
min_string = b
for i in range(l - 1, -1, -1):
if buff == 0:
if temp[i] == min_string[i] == '0':
s += '0'
buff = 0
elif temp[i] == min_string[i] == '1':
s += '0'
buff = 1
else:
s += '1'
buff = 0
else:
if temp[i] == min_string[i] == '0':
s += '1'
buff = 0
elif temp[i] == min_string[i] == '1':
s += '1'
buff = 1
else:
s += '0'
buff = 1
if not flag1:
if buff == 1:
s += '1'
elif not buff:
s += other[::-1]
else:
for j in range(len(other) - 1, -1, -1):
if other[j] == '0':
s += '1'
if j == 1:
s+=other[:1]
else:
other = other[:j]
s += other[::-1]
flag = 0
break
else:
s += '0'
if j == 0:
s += '1'
return s[::-1]``````

``````        a, b = int('0b' + a, 2), int('0b' + b, 2)
return bin(a + b)[2:]``````

emmm…我該說什么呢。哈哈哈讓我們看看一行代碼的吧！！

``        return bin(int(a,2)+int(b,2))[2:]``

int()函數用於將一個字符串或數字轉換為整型。語法：
class int(x, base)
x–字符串或數字
base–進制數，默認十進制。
bin()函數返回一個整型int或者長整數long int的二進制表示。

bin()運算返回的是二進制。所以前兩位是二進制的標志，需要[2:]去除。