You are on page 1of 6

Sub: POA

Experiment #2:

Aim of Experiment:-Write a Program to demonstrate Restoring Division


Submission Sheet

SAP ID Name of Student Date of Date of Remarks


Experiment Submission

60004190030 Dhruv Jain 4/10/21 5/10/21

Title:Restoring Division
Code:
x = int(input("Enter Dividend:- "))
y = int(input("Enter Divisor:- "))
x1 = None
y1 = None
if(x < 0):
x1 = int("{0:08b}".format(x*-1))
else:
x1 = int("{0:08b}".format(x))

if(y < 0):


y1 = int("{0:08b}".format(y*-1))
else:
y1 = int("{0:08b}".format(y))

n = len(str(x1))

def addZeroes(t,n1):
for i in range(n1-len(t)):
t="0" + t
return t

def flip(c):
return '1' if (c == '0') else '0'

def ones(t) :
temp=""
for i in range(len(t)):
temp+=flip(t[i])
return temp

def twos(t):
return bin(int( ones(t),2) + int("1",2))[2:]

#initialization
A = addZeroes("",n)
Q = str(x1)
B = addZeroes(str(y1),n)

Z = twos(B)
w=A+Q

def Compulsary(p):

p = p[1:] + "X"
print("Left shift:-",p)
p = bin(int(p[:n],2) + int(Z , 2))[2:] + p[n:]
print("A=A-B:-",p)
if len(p) == 2*n:
return p
else: return p[-2*n:]

def case1(p): #not less than 0


return p[:-1] + "1"

def case2(p): #less than 0


p = p[:-1] + "0"
p = bin(int(p[:n],2) + int(B , 2))[2:] + p[n:]
if len(p) == 2*n:
return p
else: return p[-2*n:]

#driver

for i in range(n):
w = Compulsary(w)
if w[0] == "0":
w = case1(w)
print("Positive:- ",w)
else:
w = case2(w)
print("Negative:- ",w)
print("\n")
print("Answer")
print("Remainder:-",w[:n],"(" + str(int(w[:n],2)) + ")")
q = None
if x < 0 and y > 0 or x > 0 and y < 0 :
q = int(w[n:],2)*-1

else:
q = int(w[n:],2)
print("Quotient:-",w[n:],"(" + str(q) + ")")

Screenshot1:

Screenshot2:
Screenshot3:

Screenshot4:-
Analysis:
In the restoring division method, the quotient is represented using a non-redundant number system. This is
“paper and pencil” usual algorithm. Its main characteristic is the full width comparisons required to deduce
the new quotient digit. In restoring division, the divisor is shift-positioned and subtracted from the dividend.
If subtraction of the divisor produces a negative result at any bit position relative to the dividend, the
operation at that bit position is unsuccessful, and a 0 is placed in the corresponding location of the quotient.
The divisor is added back (restored) to the result of the division operation, then the next highest bit of the
dividend is shifted into the left bit position of the result. As each bit of the dividend is shifted from right to
left, the quotient is built up from left to right. After n shifts, where n represents the number of bits in the
dividend, the division operation is complete.

Conclusion:
Thus we’ve studied and implemented the Restoring Division Algorithm successfully in C programming
language and understood that:-
• In the standard restoring division is that a test subtraction is required, the sign bit determines whether
an addition or subtraction is used.
• Also, though in this, is that an extra bit must not be maintained in the partial remainder to keep track
of the sign.
• Full width comparisons required to deduce the new quotient digit in this and also it’s slower than
other division algorithms as it requires time because of restoration in each cycle.

You might also like