Implementation of Restoring Division Algorithm for unsigned integer

In the previous article, we have already discussed the Restoring Division Algorithm. In this article, we will discuss the implementation of this algorithm.

Restoring Division Algorithm is used to divide two unsigned integers. This algorithm is used in Computer Organization and Architecture. This algorithm is called restoring because it restores the value of Accumulator(A) after each or some iterations. There is one more type i.e. Non-Restoring Division Algorithm in which value of A is not restored. Let the dividend Q = 0110 and the divisor M = 0100. The following table demonstrates the step by step solution for the given values:

Accumulator-A(0) Dividend-Q(6) Status
Initial Values 0000 0110 0100
Step 1: Left-Shift 0000 110_
A – M 1100 Unsuccessful(-ve)
Restoration 0000 1100
Step 2: Left-Shift 0001 100_
A – M 1101 Unsuccessful(-ve)
Restoration 0001 1000
Step 3: Left-Shift 0011 000_
A – M 1111 Unsuccessful(-ve)
Restoration 0011 0000
Step 4: Left-Shift 0110 000_
A – M 0010 0001 Successful(+ve)
Remainder(2) Quotient(1)

Approach: From the above solution, the idea is to observe that the number of steps required to compute the required quotient and remainder is equal to the number of bits in the dividend. Initially, let the dividend be Q and the divisor be M and the accumulator A = 0. Therefore:

  1. At each step, left shift the dividend by 1 position.
  2. Subtract the divisor from A(A – M).
  3. If the result is positive, then the step is said to be successful. In this case, the quotient bit will be “1” and the restoration is not required.
  4. If the result is negative, then the step is said to be unsuccessful. In this case, the quotient bit will be “0” and restoration is required.
  5. Repeat the above steps for all the bits of the dividend.

Note: Here, restoration is performed by adding back the divisor to A.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to divide two 
# unsigned integers using 
# Restoring Division Algorithm
  
# Function to add two binary numbers
def add(A, M):
    carry = 0
    Sum = ''
  
    # Iterating through the number
    # A. Here, it is assumed that 
    # the length of both the numbers
    # is same
    for i in range (len(A)-1, -1, -1):
  
        # Adding the values at both 
        # the indices along with the 
        # carry
        temp = int(A[i]) + int(M[i]) + carry
  
        # If the binary number exceeds 1
        if (temp>1):
            Sum += str(temp % 2)
            carry = 1
        else:
            Sum += str(temp)
            carry = 0
  
    # Returning the sum from 
    # MSB to LSB
    return Sum[::-1]    
  
# Function to find the compliment
# of the given binary number
def compliment(m):
    M = ''
  
    # Iterating through the number
    for i in range (0, len(m)):
  
        # Computing the compliment
        M += str((int(m[i]) + 1) % 2)
  
    # Adding 1 to the computed 
    # value
    M = add(M, '0001')
    return M
      
# Function to find the quotient
# and remainder using the 
# Restoring Division Algorithm
def restoringDivision(Q, M, A):
  
    # Computing the length of the
    # number
    count = len(M)
  
    # Printing the initial values
    # of the accumulator, dividend
    # and divisor
    print ('Initial Values: A:', A, 
           ' Q:', Q, ' M:', M)
  
    # The number of steps is equal to the 
    # length of the binary number
    while (count):
  
        # Printing the values at every step
        print ("\nstep:", len(M)-count + 1, end = '')
          
        # Step1: Left Shift, assigning LSB of Q 
        # to MSB of A.
        print (' Left Shift and Subtract: ', end = '')
        A = A[1:] + Q[0]
  
        # Step2: Subtract the Divisor from A 
        # (Perform A - M).
        comp_M = compliment(M)
  
        # Taking the complement of M and 
        # adding to A.
        A = add(A, comp_M)
        print(' A:', A)
        print('A:', A, ' Q:', Q[1:]+'_', end ='')
          
        if (A[0] == '1'):
  
            # The step is unsuccessful 
            # and the quotient bit 
            # will be '0'
            Q = Q[1:] + '0'
            print ('  -Unsuccessful')
  
            # Restoration of A is required
            A = add(A, M)
            print ('A:', A, ' Q:', Q, ' -Restoration')
              
        else:
  
            # Else, the step is successful 
            # and the quotient bit 
            # will be '1'
            Q = Q[1:] + '1'
            print (' Successful')
  
            # No Restoration of A.
            print ('A:', A, ' Q:',
                   Q, ' -No Restoration')
        count -= 1
  
    # Printing the final quotient 
    # and remainder of the given 
    # dividend and divisor. 
    print ('\nQuotient(Q):', Q,
           ' Remainder(A):', A)
  
  
# Driver code
if __name__ == "__main__":
  
    dividend = '0110'
    divisor = '0100'
  
    accumulator = '0' * len(dividend)
  
    restoringDivision(dividend,
                      divisor, 
                      accumulator)

chevron_right


Output:

Initial Values: A: 0000  Q: 0110  M: 0100

step: 1 Left Shift and Subtract:  A: 1100
A: 1100  Q: 110_  -Unsuccessful
A: 0000  Q: 1100  -Restoration

step: 2 Left Shift and Subtract:  A: 1101
A: 1101  Q: 100_  -Unsuccessful
A: 0001  Q: 1000  -Restoration

step: 3 Left Shift and Subtract:  A: 1111
A: 1111  Q: 000_  -Unsuccessful
A: 0011  Q: 0000  -Restoration

step: 4 Left Shift and Subtract:  A: 0010
A: 0010  Q: 000_ Successful
A: 0010  Q: 0001  -No Restoration

Quotient(Q): 0001  Remainder(A): 0010

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : amit_mangal_

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.