-2
下面的代碼也以二進制的形式添加二進制數和輸出。但是,它不會造成溢出。有任何想法嗎?如何管理溢出
program_2 = True
while program_2 == True:
bnum = input ("Please enter your first 8 digit Binary Number ")
length=len(bnum)
if length==8:
answer=((int(bnum[0]) * 128) + (int(bnum[1]) * 64) + (int(bnum[2]) * 32) + (int(bnum[3]) * 16) + (int(bnum[4]) * 8) + (int(bnum[5]) * 4) + (int(bnum[6]) * 2) + (int(bnum[7]) * 1))
bnum1 = input("Now enter a your second 8 digit Binary Number ")
length=len(bnum1)
if length==8:
answer1=((int(bnum1[0]) * 128) + (int(bnum1[1]) * 64) + (int(bnum1[2]) * 32) + (int(bnum1[3]) * 16) + (int(bnum1[4]) * 8) + (int(bnum1[5]) * 4) + (int(bnum1[6]) * 2) + (int(bnum1[7]) * 1))
total = (answer+answer1)
binary = [0,0,0,0,0,0,0,0]
for i in range (7,-1,-1):
binary[i] = total%2
total=int(total/2)
for i in range (8):
print(binary[i],end='')
print (" ")