Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de5f7b2

Browse files
Created script.py
1 parent 012291e commit de5f7b2

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

‎Restoring Divider/script.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
if len(M)==len(Q):
10+
M="0"+M
11+
else:
12+
if len(Q)>len(M):
13+
how=len(Q)-len(M)
14+
for i in range (0,how,1):
15+
M="0"+M
16+
else:
17+
how=len(M)-len(Q)
18+
for i in range (0,how-1,1):
19+
Q="0"+Q
20+
for i in range (0,len(M),1):
21+
A="0"+A
22+
size=len(M)
23+
A="0"+A
24+
M="0"+M
25+
M2=twos_complement(M)
26+
print("Solution=>")
27+
print("A=",A)
28+
print("Q=",Q)
29+
print("M=",M)
30+
print("M2=",M2)
31+
printer="A\t\tQ\t\tSize\t\tSteps"
32+
print(printer)
33+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tInitialization"
34+
print(printer)
35+
#print("A=",A)
36+
#print("Q=",Q)
37+
#print("size=",size)
38+
39+
for i in range(size,0,-1):
40+
41+
A=A[1:len(A)]+Q[0]
42+
Q=Q[1:len(Q)]
43+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tLeft Shift"
44+
print(printer)
45+
A=add(A,M2)
46+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tSubtraction"
47+
print(printer)
48+
if A[0]=='0':
49+
Q=Q+"1"
50+
else:
51+
Q=Q+"0"
52+
A=add(A,M)
53+
printer=A+"\t\t"+Q+"\t\t"+str(size)+"\t\tBit Checking"
54+
print(printer)
55+
size=size-1
56+
printer=A+"\t\t"+Q+"\t\t"+str(size)
57+
print(printer)
58+
59+
def twos_complement(n):
60+
a=""
61+
c=""
62+
for i in range(0,len(n)):
63+
if n[i]=='1':
64+
a=a+"0"
65+
else:
66+
a=a+"1"
67+
d=""
68+
for i in range (0,len(a)-1):
69+
d=d+"0"
70+
d=d+"1"
71+
c=add(a,d)
72+
return c
73+
74+
def add(x,y):
75+
carry=""
76+
result=""
77+
carry="0"
78+
for i in range(len(x)-1,-1,-1):
79+
a=carry[0]
80+
b=x[i]
81+
c=y[i]
82+
83+
if a==b and b==c and c=='0':
84+
result="0"+result
85+
carry="0"
86+
elif a==b and b==c and c=='1':
87+
result="1"+result
88+
carry="1"
89+
else:
90+
if a=='1' and b==c and c=='0':
91+
result="1"+result
92+
carry="0"
93+
elif a=='0' and b=='1' and c=='0':
94+
result="1"+result
95+
carry="0"
96+
elif a=='0' and b=='0' and c=='1':
97+
result="1"+result
98+
carry="0"
99+
elif a=='0' and b=='1' and c=='1':
100+
result="0"+result
101+
carry="1"
102+
elif a=='1' and b=='0' and c=='1':
103+
result="0"+result
104+
carry="1"
105+
elif a=='1' and b=='1' and c=='0':
106+
result="0"+result
107+
carry='1'
108+
return result
109+
110+
def left_shift(A,Q):
111+
A=A[1:len(A)]+Q[0]
112+
Q=Q[1:len(Q)]
113+
114+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /