Skip to content

Commit c50f1ef

Browse files
Merge pull request #39 from abirbhattacharya82/master
Created script.py
2 parents 6baf347 + 66832d2 commit c50f1ef

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Restoring Divider/script.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
def main():
2+
A=""
3+
Q=int(input("Enter the Dividend => "))
4+
M=int(input("Enter the Divisor => "))
5+
Q=bin(Q).replace("0b", "")
6+
M=bin(M).replace("0b", "")
7+
size=0
8+
"""
9+
This part makes the initialisation of required for the Restoring Division to occur.
10+
Which includes:
11+
1) Setting an extra to M compared to A
12+
2) Filling up A with zeroes
13+
3) Setting the size
14+
"""
15+
if len(M)==len(Q):
16+
M="0"+M
17+
else:
18+
if len(Q)>len(M):
19+
how=len(Q)-len(M)
20+
for i in range (0,how,1):
21+
M="0"+M
22+
else:
23+
how=len(M)-len(Q)
24+
for i in range (0,how-1,1):
25+
Q="0"+Q
26+
for i in range (0,len(M),1):
27+
A="0"+A
28+
size=len(M)
29+
"""
30+
The Calculation and Line by Line Display begins from here
31+
"""
32+
A="0"+A
33+
M="0"+M
34+
M2=twos_complement(M)
35+
print("Solution=>")
36+
print("A=",A)
37+
print("Q=",Q)
38+
print("M=",M)
39+
print("M2=",M2)
40+
printer="A\t\tQ\t\tSize\t\tSteps"
41+
print(printer)
42+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tInitialization" #Printing the Initialisation step
43+
print(printer)
44+
"""
45+
The division will be taking place until the size of the Divisor becomes zero
46+
"""
47+
for i in range(size,0,-1):
48+
"""
49+
Left Shift Operation
50+
"""
51+
A=A[1:len(A)]+Q[0]
52+
Q=Q[1:len(Q)]
53+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tLeft Shift"
54+
print(printer)
55+
56+
"""
57+
Subtraction
58+
"""
59+
A=add(A,M2)
60+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tSubtraction"
61+
print(printer)
62+
63+
"""
64+
Bit Checking and AAddition if required
65+
"""
66+
if A[0]=='0':
67+
Q=Q+"1"
68+
else:
69+
Q=Q+"0"
70+
A=add(A,M)
71+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tBit Checking"
72+
print(printer)
73+
74+
"""
75+
Decreasing Size
76+
"""
77+
size=size-1
78+
printer=A+"\t\t"+Q+"\t\t"+str(size)
79+
print(printer)
80+
81+
def twos_complement(n):
82+
a=""
83+
c=""
84+
"""
85+
Performing 1's Complement by changing all zeroes to one
86+
"""
87+
for i in range(0,len(n)):
88+
if n[i]=='1':
89+
a=a+"0"
90+
else:
91+
a=a+"1"
92+
"""
93+
Performing 2's complement by adding 1 to the 1's complement
94+
"""
95+
d=""
96+
for i in range (0,len(a)-1):
97+
d=d+"0"
98+
d=d+"1"
99+
c=add(a,d)
100+
return c
101+
102+
def add(x,y):
103+
"""
104+
Binary Adddition bing carried out
105+
"""
106+
carry=""
107+
result=""
108+
carry="0"
109+
for i in range(len(x)-1,-1,-1):
110+
a=carry[0]
111+
b=x[i]
112+
c=y[i]
113+
114+
if a==b and b==c and c=='0':
115+
result="0"+result
116+
carry="0"
117+
elif a==b and b==c and c=='1':
118+
result="1"+result
119+
carry="1"
120+
else:
121+
if a=='1' and b==c and c=='0':
122+
result="1"+result
123+
carry="0"
124+
elif a=='0' and b=='1' and c=='0':
125+
result="1"+result
126+
carry="0"
127+
elif a=='0' and b=='0' and c=='1':
128+
result="1"+result
129+
carry="0"
130+
elif a=='0' and b=='1' and c=='1':
131+
result="0"+result
132+
carry="1"
133+
elif a=='1' and b=='0' and c=='1':
134+
result="0"+result
135+
carry="1"
136+
elif a=='1' and b=='1' and c=='0':
137+
result="0"+result
138+
carry='1'
139+
return result
140+
main()

0 commit comments

Comments
 (0)