0% found this document useful (0 votes)
12 views6 pages

Experiment 7

Uploaded by

Advay Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Experiment 7

Uploaded by

Advay Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT ASSESSMENT

ACADEMIC YEAR 2024-25

Course: Digital Logic & Computer Organization and Architecture Lab Course code:
CSL304 Year: S.E. SEM: III

Experiment No. 7
To implement non-restoring division algorithm using Java-
programming.
Name: ADVAY JOSHI
Roll Number: SE2/S1/3
Date of Performance: 03/09/24
Date of Submission: 17/09/24

PERFORMANCE INDICATORS 1 2 3 TOTAL

A. Execution (5) 3 4 5

B. Journal Work and Timely Submission (5) 3 4 5

C. Understanding (10) 8 9 10

Total (out of 20 marks)

Exceed Expectations (3) Meet Expectations (2) Below Expectations (1)

Signature of the Teacher:

Name of the Teacher: _Chinmay Vanjare Date:


Experiment No. 7
Aim: To implement non-restoring division algorithm using Java-programming.

Objective -
1. To understand the working of non-restoring division algorithm.
2. To understand how to implement non-restoring division algorithm using java-
programming.

Theory:

Step-1: First the registers are initialized with corresponding values (Q = Dividend, M =
Divisor, A = 0, n = number of bits in dividend)
Step-2: Check the sign bit of register A
Step-3: If it is 1 shift left content of AQ and perform A = A+M, otherwise shift left AQ and
perform A = A-M (means add 2’s complement of M to A and store it to A)
Step-4: Again the sign bit of register A
Step-5: If sign bit is 1 Q[0] become 0 otherwise Q[0] become 1 (Q[0] means least significant
bit of register Q)
Step-6: Decrements value of N by 1
Step-7: If N is not equal to zero go to Step 2 otherwise go to next step
Step-8: If sign bit of A is 1 then perform A = A+M
Step-9: Register Q contains quotient and A contains remainder.

Flowchart-
x

Program-

// 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);
}
}

Output –

Conclusion - The Non-restoring Division Algorithm is a reliable and efficient way to


perform division on unsigned integers in Java. It is easy to implement and understand, and it
is suitable for a variety of applications.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy