add code for binary multiplication, method 4
This commit is contained in:
commit
de5f5a70df
|
@ -0,0 +1 @@
|
|||
*__pycache__*
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,89 @@
|
|||
def align_binary_to_right(value, size):
|
||||
if "b" in value:
|
||||
result = value.split("b")[1]
|
||||
else:
|
||||
result = str(value)
|
||||
|
||||
return result.rjust(size, "0")
|
||||
|
||||
al = align_binary_to_right
|
||||
|
||||
def multiply(x, y, method):
|
||||
if method == 4:
|
||||
n = len(bin(x)[2:]) # base registry bit length, usually written as n
|
||||
# every table line has registers like so: RG1, RG3, RG2
|
||||
data_table = [[["0"*(2*n+1), "0" + bin(y)[2:] + "0"*n, bin(x)[2:], "-"]]*2]
|
||||
print(f"Starting with: {data_table}")
|
||||
print(data_table[-1][-1][2])
|
||||
print(int('0b' + data_table[-1][-1][2], 2))
|
||||
while int('0b' + data_table[-1][-1][2], 2) != 0:
|
||||
data_table.append([])
|
||||
'''
|
||||
if data_table[-2][-1][2][0] == "1":
|
||||
data_table[-1].append([
|
||||
al(bin(int("0b"+data_table[-2][-1][0], 2) + int("0b"+data_table[-2][-1][1], 2))[-(2*n+1):], 2*n+1), # RG1 + RG3
|
||||
data_table[-2][-1][1],
|
||||
data_table[-2][-1][2],
|
||||
])
|
||||
|
||||
data_table[-1].append([
|
||||
data_table[-1][-1][0],
|
||||
'0' + data_table[-1][-1][1][:-1], # 0.r(RG3)
|
||||
data_table[-2][-1][2][1:] + '0', # l(RG2).0
|
||||
])
|
||||
'''
|
||||
|
||||
'''
|
||||
if data_table[-2][-1][2][0] == "1":
|
||||
data_table[-1].append([
|
||||
al(bin(int("0b"+data_table[-2][-1][0], 2) + int("0b"+data_table[-2][-1][1], 2))[-(2*n+1):], 2*n+1), # RG1 + RG3
|
||||
'0' + data_table[-2][1][:-1],
|
||||
data_table[-2][2][1:] + '0', # l(RG2).0
|
||||
'''
|
||||
|
||||
if data_table[-2][-1][2][0] == "1":
|
||||
data_table[-1].append([
|
||||
al(bin(int("0b"+data_table[-2][-1][0], 2) + int("0b"+data_table[-2][-1][1], 2))[-(2*n+1):], 2*n+1), # RG1 + RG3
|
||||
data_table[-2][-1][1],
|
||||
data_table[-2][-1][2],
|
||||
"RG1+RG3"
|
||||
])
|
||||
|
||||
data_table[-1].append([
|
||||
data_table[-1][-1][0],
|
||||
'0' + data_table[-1][-1][1][:-1], # 0.r(RG3)
|
||||
data_table[-1][-1][2][1:] + '0', # l(RG2).0
|
||||
"0.r(RG3), l(RG2).0"
|
||||
])
|
||||
|
||||
else:
|
||||
data_table[-1].append([
|
||||
data_table[-2][-1][0],
|
||||
'0' + data_table[-2][-1][1][:-1], # 0.r(RG3)
|
||||
data_table[-2][-1][2][1:] + '0', # l(RG2).0
|
||||
"0.r(RG3), l(RG2).0"
|
||||
])
|
||||
|
||||
|
||||
print(data_table)
|
||||
|
||||
return data_table
|
||||
|
||||
if __name__ == "__main__":
|
||||
x = int("0b" + input("X: "), 2)
|
||||
y = int("0b" + input("Y: "), 2)
|
||||
method = int(input("Method: "))
|
||||
dt = multiply(x, y, method)
|
||||
|
||||
from lib.prettytable import PrettyTable
|
||||
pt = PrettyTable()
|
||||
pt.field_names = ["RG1", "RG3", "RG2", "Operations"]
|
||||
|
||||
for i in dt[1:]:
|
||||
for j in range(len(i)):
|
||||
if j+1 == len(i):
|
||||
pt.add_row(i[j], divider = True)
|
||||
else:
|
||||
pt.add_row(i[j])
|
||||
|
||||
print(pt)
|
Loading…
Reference in New Issue