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