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

Non-restoring division algorithm is used to divide two unsigned integers. The other form of this algorithm is Restoring Division. This algorithm is different from the other algorithm because here, there is no concept of restoration and this algorithm is less complex than the restoring division algorithm. 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 | 0111 | 0101(M) |

Step1:Left-Shift | 0000 | 111_ | |

Operation:A – M | 1011 | 1110 | Unsuccessful(-ve) A+M in Next Step |

Step2:Left-Shift | 0111 | 110_ | |

Operation:A + M | 1100 | 1100 | Unsuccessful(-ve) A+M in Next Step |

Step3:Left-Shift | 1001 | 100_ | |

Operation:A + M | 1110 | 1000 | Unsuccessful(-ve) A+M in Next Step |

Step4:Left-Shift | 1101 | 000_ | |

Operation: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:

- At each step, left shift the dividend by 1 position.
- Subtract the divisor from A (A – M).
- 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. So, the next step will also be subtraction.
- If the result is negative then the step is said to be “unsuccessful”. In this case, the quotient bit will be “0”. Here, the restoration is NOT performed like the restoration division algorithm. Instead, the next step will be ADDITION in place of subtraction.
- Repeat steps 1 to 4 for all bits of the Dividend.

Below is the implementation of the above approach:

`# Python program to divide two ` `# unsigned integers using ` `# Non-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 ` `# Non-Restoring Division Algorithm` `def` `nonRestoringDivision(Q, M, A):` ` ` ` ` `# Computing the length of the` ` ` `# number` ` ` `count ` `=` `len` `(M)` ` ` ` ` ` ` `comp_M ` `=` `compliment(M)` ` ` ` ` `# Variable to determine whether ` ` ` `# addition or subtraction has ` ` ` `# to be computed for the next step` ` ` `flag ` `=` `'successful'` ` ` ` ` `# 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 '` `, end ` `=` `'')` ` ` `A ` `=` `A[` `1` `:] ` `+` `Q[` `0` `]` ` ` ` ` `# Choosing the addition` ` ` `# or subtraction based on the` ` ` `# result of the previous step` ` ` `if` `(flag ` `=` `=` `'successful'` `):` ` ` `A ` `=` `add(A, comp_M)` ` ` `print` `(` `'subtract: '` `)` ` ` `else` `:` ` ` `A ` `=` `add(A, M)` ` ` `print` `(` `'Addition: '` `)` ` ` ` ` `print` `(` `'A:'` `, A, ` `' Q:'` `, ` ` ` `Q[` `1` `:]` `+` `'_'` `, end ` `=` `'')` ` ` ` ` `if` `(A[` `0` `] ` `=` `=` `'1'` `):` ` ` ` ` ` ` `# Step is unsuccessful and the ` ` ` `# quotient bit will be '0'` ` ` `Q ` `=` `Q[` `1` `:] ` `+` `'0'` ` ` `print` `(` `' -Unsuccessful'` `)` ` ` ` ` `flag ` `=` `'unsuccessful'` ` ` `print` `(` `'A:'` `, A, ` `' Q:'` `, Q, ` ` ` `' -Addition in next Step'` `)` ` ` ` ` `else` `:` ` ` ` ` `# Step is successful and the quotient ` ` ` `# bit will be '1'` ` ` `Q ` `=` `Q[` `1` `:] ` `+` `'1'` ` ` `print` `(` `' Successful'` `)` ` ` ` ` `flag ` `=` `'successful'` ` ` `print` `(` `'A:'` `, A, ` `' Q:'` `, Q, ` ` ` `' -Subtraction in next step'` `)` ` ` `count ` `-` `=` `1` ` ` `print` `(` `'\nQuotient(Q):'` `, Q, ` ` ` `' Remainder(A):'` `, A)` ` ` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `dividend ` `=` `'0111'` ` ` `divisor ` `=` `'0101'` ` ` ` ` `accumulator ` `=` `'0'` `*` `len` `(dividend)` ` ` ` ` `nonRestoringDivision(dividend,` ` ` `divisor, ` ` ` `accumulator)` |

**Output:**

Initial Values: A: 0000 Q: 0111 M: 0101 step: 1 Left Shift and subtract: A: 1011 Q: 111_ -Unsuccessful A: 1011 Q: 1110 -Addition in next Step step: 2 Left Shift and Addition: A: 1100 Q: 110_ -Unsuccessful A: 1100 Q: 1100 -Addition in next Step step: 3 Left Shift and Addition: A: 1110 Q: 100_ -Unsuccessful A: 1110 Q: 1000 -Addition in next Step step: 4 Left Shift and Addition: A: 0010 Q: 000_ Successful A: 0010 Q: 0001 -Subtraction in next step 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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**