Here is function which takes 2 binary numbers(str) in form of 8 signs(for example 4=00000100). in formula, we have to reverse second number and for each bit separately do next actions:
looks like: 3*4 = 12: 00000011(bin1) * 00000100(bin2) = 00000000 00001100 (result) steps: we take reversed bin2 and observe each bit
Please, I would be very grateful for any help, I'm really struggling with this task.
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
I tried to change this code but it is correct as it seems to me according to logic which I wrote before.
You have lots of problems with your code:
younger_result_list
. This is supposed to hold the low order result.elder_result_list
, but are updating it using forward indexes.elder_result_list.pop(0)
is removing results from the wrong end.I have rewritten it like this where I use a temporary list result_list
to hold the results of the addition and I remove the correct bit from elder_result_list
and add it to 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
Sample code to calculate 42*42=1764:
result = result_first_diapazone('00101010', '00101010')
print(result)
Output:
00000110 11100100
Update:
addition_table = {
('0', '0', '0'): ('0', '0'),
('0', '0', '1'): ('1', '0'),
('0', '1', '0'): ('1', '0'),
('0', '1', '1'): ('0', '1'),
('1', '0', '0'): ('1', '0'),
('1', '0', '1'): ('0', '1'),
('1', '1', '0'): ('0', '1'),
('1', '1', '1'): ('1', '1')
}
def binary_add(augend, addend):
result = ['0'] * 8
carry = '0'
for i, (x, y) in enumerate(zip(augend, addend)):
result[i], carry = addition_table[(x, y, carry)]
return result, carry
def binary_multiply(multiplier, multiplicand):
upper_result_list = ['0'] * 8
lower_result_list = []
for bit in multiplier:
if bit == '1':
upper_result_list, carry = binary_add(multiplicand, upper_result_list)
upper_result_list.append(carry)
else:
upper_result_list.append('0')
low = upper_result_list.pop(0)
lower_result_list.append(low)
return upper_result_list, lower_result_list
def l_r(i):
return list(reversed(i))
def result_first_diapazone(multiplier, multiplicand):
upper, lower = binary_multiply(l_r(multiplier), l_r(multiplicand))
return ''.join(reversed(upper))+ ' ' + ''.join(reversed(lower))
Output as above.
Note that my first code sample gives the wrong output in certain cases because the carry
variable is defined too early. It should be inside the first for loop, not at the start. My updated code gets it right since it is part of the binary_add()
function.