# Implementation of Non-Restoring Division Algorithm for Unsigned Integer

Last Updated : 16 Mar, 2023

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:

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. So, the next step will also be subtraction.
4. 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.
5. Repeat steps 1 to 4 for all bits of the Dividend.

Below is the implementation of the above approach:

## C++

 `// C++ program to divide two` `// unsigned integers using` `// Non-Restoring Division Algorithm`   `#include ` `#include ` `using` `namespace` `std;`   `// Function to add two binary numbers` `string add(string A, string M)` `{` `    ``int` `carry = 0;` `    ``string Sum = ``""``; ``// Iterating through the number` `    ``// A. Here, it is assumed that` `    ``// the length of both the numbers` `    ``// is same` `    ``for` `(``int` `i = A.length() - 1; i >= 0; i--) {` `        ``// Adding the values at both` `        ``// the indices along with the` `        ``// carry` `        ``int` `temp = (A[i] - ``'0'``) + (M[i] - ``'0'``) + carry;`   `        ``// If the binary number exceeds 1` `        ``if` `(temp > 1) {` `            ``Sum += to_string(temp % 2);` `            ``carry = 1;` `        ``}` `        ``else` `{` `            ``Sum += to_string(temp);` `            ``carry = 0;` `        ``}` `    ``}`   `    ``// Returning the sum from` `    ``// MSB to LSB` `    ``return` `string(Sum.rbegin(), Sum.rend());` `}`   `// Function to find the compliment` `// of the given binary number` `string compliment(string m)` `{` `    ``string M = ``""``; ``// Iterating through the number` `    ``for` `(``int` `i = 0; i < m.length(); i++) {` `        ``// Computing the compliment` `        ``M += to_string((m[i] - ``'0'` `+ 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` `void` `nonRestoringDivision(string Q, string M, string A)` `{` `    ``// Computing the length of the` `    ``// number` `    ``int` `count = M.length();` `    ``string comp_M = compliment(M);`   `    ``// Variable to determine whether` `    ``// addition or subtraction has` `    ``// to be computed for the next step` `    ``string flag = ``"successful"``;`   `    ``// Printing the initial values` `    ``// of the accumulator, dividend` `    ``// and divisor` `    ``cout << ``"Initial Values: A: "` `<< A << ``" Q: "` `<< Q` `         ``<< ``" M: "` `<< M << endl;`   `    ``// The number of steps is equal to the` `    ``// length of the binary number` `    ``while` `(count) {` `        ``// Printing the values at every step` `        ``cout << ``"\nstep: "` `<< M.length() - count + 1;`   `        ``// Step1: Left Shift, assigning LSB of Q` `        ``// to MSB of A.` `        ``cout << ``" Left Shift and "``;`   `        ``A = A.substr(1) + Q[0];`   `        ``// Choosing the addition` `        ``// or subtraction based on the` `        ``// result of the previous step` `        ``if` `(flag == ``"successful"``) {` `            ``A = add(A, comp_M);` `            ``cout << ``"subtract: "``;` `        ``}` `        ``else` `{` `            ``A = add(A, M);` `            ``cout << ``"Addition: "``;` `        ``}`   `        ``cout << ``"A: "` `<< A << ``" Q: "` `<< Q.substr(1) << ``"_"``;`   `        ``if` `(A[0] == ``'1'``) {` `            ``// Step is unsuccessful and the` `            ``// quotient bit will be '0'` `            ``Q = Q.substr(1) + ``"0"``;` `            ``cout << ``" -Unsuccessful"``;`   `            ``flag = ``"unsuccessful"``;` `            ``cout << ``" A: "` `<< A << ``" Q: "` `<< Q` `                 ``<< ``" -Addition in next Step"` `<< endl;` `        ``}` `        ``else` `{` `            ``// Step is successful and the quotient` `            ``// bit will be '1'` `            ``Q = Q.substr(1) + ``"1"``;` `            ``cout << ``" Successful"``;` `            ``flag = ``"successful"``;` `            ``cout << ``" A: "` `<< A << ``" Q: "` `<< Q` `                 ``<< ``" -Subtraction in next step"` `<< endl;` `        ``}` `        ``count--;` `    ``}` `    ``cout << ``"\nQuotient(Q): "` `<< Q << ``" Remainder(A): "` `<< A` `         ``<< endl;` `}`   `// Driver code` `int` `main()` `{` `    ``string dividend = ``"0111"``;` `    ``string divisor = ``"0101"``;` `    ``string accumulator = string(dividend.size(), ``'0'``);` `    ``nonRestoringDivision(dividend, divisor, accumulator);`   `    ``return` `0;` `}`

## Java

 `// This class implements non-restoring division algorithm` `public` `class` `NonRestoringDivision {` `// This method performs binary addition of two binary numbers` `public` `static` `String add(String A, String M) {` `    ``int` `carry = ``0``;` `    ``StringBuilder sum = ``new` `StringBuilder();`   `    ``// Start from the least significant bit and go up to the most significant bit` `    ``for` `(``int` `i = A.length()-``1``; i >= ``0``; i--) {` `        ``// Calculate the sum of two bits and the carry` `        ``int` `temp = Character.getNumericValue(A.charAt(i)) + Character.getNumericValue(M.charAt(i)) + carry;`   `        ``// If the sum is greater than 1, append the least significant bit to the sum and set the carry to 1` `        ``if` `(temp > ``1``) {` `            ``sum.append(temp % ``2``);` `            ``carry = ``1``;` `        ``}` `        ``// Otherwise, append the sum to the result and set the carry to 0` `        ``else` `{` `            ``sum.append(temp);` `            ``carry = ``0``;` `        ``}` `    ``}`   `    ``// Reverse the result and return as a string` `    ``return` `sum.reverse().toString();` `}`   `// This method calculates the 2's complement of a binary number` `public` `static` `String compliment(String m) {` `    ``StringBuilder M = ``new` `StringBuilder();`   `    ``// Invert each bit of the input string` `    ``for` `(``int` `i = ``0``; i < m.length(); i++) {` `        ``M.append((Character.getNumericValue(m.charAt(i)) + ``1``) % ``2``);` `    ``}`   `    ``// Add 1 to the inverted value and return as a string` `    ``M = ``new` `StringBuilder(add(M.toString(), ``"0001"``));` `    ``return` `M.toString();` `}`   `// This method performs non-restoring division algorithm` `public` `static` `void` `nonRestoringDivision(String Q, String M, String A) {` `    ``int` `count = M.length();` `    ``String comp_M = compliment(M);` `    ``String flag = ``"successful"``;`   `    ``// Print the initial values of A, Q, and M` `    ``System.out.println(``"Initial Values: A: "` `+ A + ``" Q: "` `+ Q + ``" M: "` `+ M);`   `    ``// Repeat the division process for each bit of M` `    ``while` `(count > ``0``) {` `        ``// Shift the contents of A and Q to the left by one bit` `        ``System.out.print(``"\nstep: "` `+ (M.length() - count + ``1``) + ``" Left Shift and "``);` `        ``A = A.substring(``1``) + Q.charAt(``0``);`   `        ``// If the previous step was successful, subtract M from A; otherwise, add M to A` `        ``if` `(flag.equals(``"successful"``)) {` `            ``A = add(A, comp_M);` `            ``System.out.print(``"subtract: "``);` `        ``}` `        ``else` `{` `            ``A = add(A, M);` `            ``System.out.print(``"Addition: "``);` `        ``}`   `        ``// Update the value of Q based on the sign of A and print the current values of A and Q` `        ``System.out.print(``"A: "` `+ A + ``" Q: "` `+ Q.substring(``1``) + ``"_"``);` `        ``if` `(A.charAt(``0``) == ``'1'``) {` `            ``Q = Q.substring(``1``) + ``'0'``;` `            ``System.out.println(``" -Unsuccessful"``);` `            ``flag = ``"unsuccessful"``;` `            ``System.out.println(``"A: "` `+ A + ``" Q: "` `+ Q + ``" -Addition in next Step"``);` `        ``}` `        ``else` `{` `            ``Q = Q.substring(``1``) + ``'1'``;` `            ``System.out.println(``" Successful"``);` `            ``flag = ``"successful"``;` `            ``System.out.println(``"A: "` `+ A + ``" Q: "` `+ Q + ``" -Subtraction in next step"``);` `        ``}`   `        ``// Decrement count` `            ``count--;` `        ``}`   `        ``System.out.println(``"\nQuotient(Q): "` `+ Q + ``" Remainder(A): "` `+ A);` `    ``}` `//Driver code` `    ``public` `static` `void` `main(String[] args) {` `        ``String dividend = ``"0111"``;` `        ``String divisor = ``"0101"``;` `        ``String accumulator = ``"0"``.repeat(dividend.length());`   `        ``nonRestoringDivision(dividend, divisor, accumulator);` `    ``}` `}`

## Python3

 `# 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)`

## C#

 `// C# program to divide two` `// unsigned integers using` `// Non-Restoring Division Algorithm`   `using` `System;`   `class` `Program` `{` `    ``// Function to add two binary numbers` `    ``static` `string` `Add(``string` `A, ``string` `M)` `    ``{` `        ``int` `carry = 0;` `        ``string` `Sum = ``""``;` `        `  `        ``// Iterating through the number` `        ``// A. Here, it is assumed that` `        ``// the length of both the numbers` `        ``// is same` `        ``for` `(``int` `i = A.Length - 1; i >= 0; i--)` `        ``{` `            `  `            ``// Adding the values at both` `            ``// the indices along with the` `            ``// carry` `            ``int` `temp = ``int``.Parse(A[i].ToString()) + ``int``.Parse(M[i].ToString()) + carry;`   `            ``// If the binary number exceeds 1` `            ``if` `(temp > 1)` `            ``{` `                ``Sum += (temp % 2).ToString();` `                ``carry = 1;` `            ``}` `            ``else` `            ``{` `                ``Sum += temp.ToString();` `                ``carry = 0;` `            ``}` `        ``}`   `        ``char``[] charArray = Sum.ToCharArray();` `        ``Array.Reverse(charArray);`   `        ``// Returning the sum from` `        ``// MSB to LSB` `        ``return` `new` `string``(charArray);` `    ``}` `    `  `    `  `    ``// Function to find the compliment` `    ``// of the given binary number` `    ``static` `string` `Compliment(``string` `m)` `    ``{` `        ``string` `M = ``""``;`   `        ``// Iterating through the number` `        ``for` `(``int` `i = 0; i < m.Length; i++)` `        ``{` `            ``// Computing the compliment` `            ``M += ((``int``.Parse(m[i].ToString()) + 1) % 2).ToString();` `        ``}`   `        ``// 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` `    ``static` `void` `NonRestoringDivision(``string` `Q, ``string` `M, ``string` `A)` `    ``{` `        `  `        ``// Computing the length of the` `        ``// number` `        ``int` `count = M.Length;`   `        ``string` `comp_M = Compliment(M);` `        `  `        `  `        ``// Variable to determine whether` `        ``// addition or subtraction has` `        ``// to be computed for the next step` `        ``string` `flag = ``"successful"``;` `        `  `        ``// Printing the initial values` `        ``// of the accumulator, dividend` `        ``// and divisor` `        ``Console.WriteLine(``"Initial Values: A: {0}, Q: {1}, M: {2}"``, A, Q, M);` `        `  `        ``// The number of steps is equal to the` `        ``// length of the binary number` `        ``while` `(count > 0)` `        ``{` `            ``// Printing the values at every step` `            ``// Step1: Left Shift, assigning LSB of Q` `            ``// to MSB of A.` `            ``Console.Write(``"\nstep: {0} Left Shift and "``, M.Length - count + 1);`   `            ``A = A.Substring(1) + Q[0];`   `            ``// Choosing the addition` `            ``// or subtraction based on the` `            ``// result of the previous step` `            ``if` `(flag == ``"successful"``)` `            ``{` `                ``A = Add(A, comp_M);` `                ``Console.Write(``"subtract: "``);` `            ``}` `            ``else` `            ``{` `                ``A = Add(A, M);` `                ``Console.Write(``"Addition: "``);` `            ``}`   `            ``Console.Write(``"A: {0}, Q: {1}_{2}"``, A, Q.Substring(1), ``""``);`   `            ``if` `(A[0] == ``'1'``)` `            ``{` `                `  `                ``// Step is unsuccessful and the` `                ``// quotient bit will be '0'` `                ``Q = Q.Substring(1) + ``'0'``;` `                ``Console.WriteLine(``" -Unsuccessful"``);` `                ``flag = ``"unsuccessful"``;` `                ``Console.WriteLine(``"A: {0}, Q: {1} -Addition in next Step"``, A, Q);` `            ``}` `            ``else` `            ``{` `                `  `                ``// Step is successful and the quotient` `                ``// bit will be '1'` `                ``Q = Q.Substring(1) + ``'1'``;` `                ``Console.WriteLine(``" Successful"``);` `                ``flag = ``"successful"``;` `                ``Console.WriteLine(``"A: {0}, Q: {1} -Subtraction in next step"``, A, Q);` `            ``}`   `            ``count--;` `        ``}`   `        ``Console.WriteLine(``"\nQuotient(Q): {0}, Remainder(A): {1}"``, Q, A);` `    ``}`   `    ``// Driver code` `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``string` `dividend = ``"0111"``;` `        ``string` `divisor = ``"0101"``;`   `        ``string` `accumulator = ``new` `string``(``'0'``, dividend.Length);`   `        ``NonRestoringDivision(dividend, divisor, accumulator);` `    ``}` `}`     `//This code contributed by shivhack999`

## Javascript

 `// JavaScript program to divide two ` `// unsigned integers using ` `// Non-Restoring Division Algorithm`   `// Function to add two binary numbers` `function` `add(A, M)` `{` `    ``let carry = 0` `    ``let Sum = ``''`   `    ``// Iterating through the number` `    ``// A. Here, it is assumed that ` `    ``// the length of both the numbers` `    ``// is same` `    ``for` `(``var` `i = A.length - 1; i > -1; i--)` `    ``{`   `        ``// Adding the values at both ` `        ``// the indices along with the ` `        ``// carry` `        ``let temp = parseInt(A[i]) + parseInt(M[i]) + carry`   `        ``// If the binary number exceeds 1` `        ``if` `(temp > 1)` `        ``{` `            ``Sum += (temp % 2).toString()` `            ``carry = 1` `        ``}` `        ``else` `        ``{` `            ``Sum += (temp).toString()` `            ``carry = 0` `        ``}` `    ``}`   `    ``// Returning the sum from ` `    ``// MSB to LSB` `    `  `    ``return` `Sum.split(``""``).reverse().join(``""``)` `}`   `// Function to find the compliment` `// of the given binary number` `function` `compliment(m)` `{` `    ``let M = ``''`   `    ``// Iterating through the number` `    ``for` `(``var` `i = 0; i < m.length; i++)`   `        ``// Computing the compliment` `        ``M += ((parseInt(m.charAt(i)) + 1) % 2).toString()`   `    ``// 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` `function` `nonRestoringDivision(Q, M, A)` `{` `    `  `    ``// Computing the length of the` `    ``// number` `    ``let count = (M).length`     `    ``let comp_M = compliment(M)`   `    ``// Variable to determine whether ` `    ``// addition or subtraction has ` `    ``// to be computed for the next step` `    ``let flag = ``'successful'`      `    ``// Printing the initial values` `    ``// of the accumulator, dividend` `    ``// and divisor` `    ``console.log (``'Initial Values: A:'``, A, ` `           ``' Q:'``, Q, ``' M:'``, M)` `    `  `    ``// The number of steps is equal to the ` `    ``// length of the binary number` `    ``while` `(count > 0)` `    ``{` `        ``Q = Q.split()` `        ``// Printing the values at every step` `        ``process.stdout.write (``"\n step: "` `+ (M.length-count + 1))` `        `  `        ``// Step1: Left Shift, assigning LSB of Q ` `        ``// to MSB of A.` `        ``process.stdout.write (``' Left Shift and '``)` `        ``A = Array.from(A)` `        ``A.shift()` `        ``A.push(Q[0])` `        ``A = A.join(``""``)`   `        ``// Choosing the addition` `        ``// or subtraction based on the` `        ``// result of the previous step` `        ``if` `(flag == ``'successful'``)` `        ``{` `            ``A = add(A, comp_M)` `            ``console.log (``'subtract: '``)` `        ``}` `        ``else` `        ``{` `            ``A = add(A, M)` `            ``console.log (``'Addition: '``)` `        ``}` `        `  `        ``Q.shift()` `        `  `        ``Q = Q.join(``""``)` `        ``process.stdout.write(``'A: '``+ A + ``' Q: '` `+ Q + ``" "``)`   `        ``if` `(A[0] == ``'1'``)` `        ``{`   `            ``// Step is unsuccessful and the ` `            ``// quotient bit will be '0'` `            ``Q = Q + ``'0'` `            ``console.log (``'  -Unsuccessful'``)`   `            ``flag = ``'unsuccessful'` `            ``console.log (``'A:'``, A, ``' Q:'``, Q, ` `                   ``' -Addition in next Step'``)` `        ``}` `            `  `        ``else` `        ``{` `            ``// Step is successful and the quotient ` `            ``// bit will be '1'` `            ``Q = Q + ``'1'` `            ``console.log (``'  Successful'``)`   `            ``flag = ``'successful'` `            ``console.log (``'A:'``, A, ``' Q:'``, Q, ` `                   ``' -Subtraction in next step'``)` `        ``}` `        ``count -= 1` `    ``}` `    ``console.log (``'\nQuotient(Q):'``, Q,  ``' Remainder(A):'``, A)` `}`   `// Driver code` `let dividend = ``'0111'` `let divisor = ``'0101'` ` `  `let accumulator = ``'0'` `* (dividend).length` ` `  `nonRestoringDivision(dividend,` `                         ``divisor, ` `                         ``accumulator)`   `// This code is contributed by phasing17.`

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```