这是一个函数,采用8个符号形式的2个二进制数(str)(例如4=00000100)。 在公式中,我们必须反转第二个数字,并为每一位分别执行下一步操作:
看起来像:3*4 = 12:00000011(bin1)* 00000100(bin2)= 00000000 00001100(结果) 步骤:我们取反转的 bin2 并观察每一位
拜托,如果有任何帮助,我将非常感激,我真的很难完成这项任务。
def result_first_diapazone(bin1, bin2):
carry = 0
elder_result_list = ['0', '0', '0', '0', '0', '0', '0', '0']
younger_result_list = ['0', '0', '0', '0', '0', '0', '0', '0']
bin1_list = list(reversed(bin1))
bin2_list = list(reversed(bin2))
for bit in bin2_list:
if bit == '1':
for i, (x, y) in enumerate(zip(bin1_list, reversed(elder_result_list))):
if (x, y) == ('0', '0') and carry == 0:
elder_result_list[i] = '0'
carry = 0
elif (x, y) == ('0', '0') and carry == 1:
elder_result_list[i] = '1'
carry = 0
elif (x, y) == ('1', '0') and carry == 0:
elder_result_list[i] = '1'
carry = 0
elif (x, y) == ('1', '0') and carry == 1:
elder_result_list[i] = '0'
carry = 1
elif (x, y) == ('0', '1') and carry == 0:
elder_result_list[i] = '1'
carry = 0
elif (x, y) == ('0', '1') and carry == 1:
elder_result_list[i] = '0'
carry = 1
elif (x, y) == ('1', '1') and carry == 0:
elder_result_list[i] = '0'
carry = 1
elif (x, y) == ('1', '1') and carry == 1:
elder_result_list[i] = '1'
carry = 1
else:
elder_result_list[i] = '1'
carry = 1
elder_result_list.append(str(carry))
elder_result_list.pop(0)
else:
elder_result_list.append('0')
elder_result_list.pop(0)
elder_result_list = reversed(elder_result_list)
elder_result_str = ''.join(map(str, elder_result_list))
younger_result_str = ''.join(map(str, younger_result_list))
result_result = elder_result_str + ' ' + younger_result_str
return result_result
我尝试更改此代码,但它是正确的,因为根据我之前编写的逻辑,它似乎是正确的。
您的代码有很多问题:
younger_result_list
。这应该保存低阶结果。elder_result_list
,但使用前向索引更新它。elder_result_list.pop(0)
正在删除错误一端的结果。我像这样重写了它,我使用临时列表
result_list
来保存加法的结果,然后从elder_result_list
中删除正确的位并将其添加到younger_result_list
:
def result_first_diapazone(bin1, bin2):
carry = 0
elder_result_list = ['0', '0', '0', '0', '0', '0', '0', '0']
younger_result_list = []
bin1_list = list(reversed(bin1))
bin2_list = list(reversed(bin2))
for bit in bin2_list:
if bit == '1':
result_list = []
for i, (x, y) in enumerate(zip(bin1_list, reversed(elder_result_list))):
if (x, y) == ('0', '0') and carry == 0:
result_list.append('0')
carry = 0
elif (x, y) == ('0', '0') and carry == 1:
result_list.append('1')
carry = 0
elif (x, y) == ('1', '0') and carry == 0:
result_list.append('1')
carry = 0
elif (x, y) == ('1', '0') and carry == 1:
result_list.append('0')
carry = 1
elif (x, y) == ('0', '1') and carry == 0:
result_list.append('1')
carry = 0
elif (x, y) == ('0', '1') and carry == 1:
result_list.append('0')
carry = 1
elif (x, y) == ('1', '1') and carry == 0:
result_list.append('0')
carry = 1
elif (x, y) == ('1', '1') and carry == 1:
result_list.append('1')
carry = 1
else:
result_list.append('1')
carry = 1
result_list.append(str(carry))
elder_result_list = list(reversed(result_list))
else:
elder_result_list.insert(0,'0')
low = elder_result_list.pop()
younger_result_list.append(low)
elder_result_str = ''.join(map(str, elder_result_list))
younger_result_str = ''.join(map(str, reversed(younger_result_list)))
result_result = elder_result_str + ' ' + younger_result_str
return result_result
计算 42*42=1764 的示例代码:
result = result_first_diapazone('00101010', '00101010')
print(result)
输出:
00000110 11100100