# I code a binary division in java, but the result is not desirable.

• 03-09-2011, 02:15 PM
I code a binary division in java, but the result is not desirable.
I tried to do a code in java of binary division, but when i tried to run it by taking a example from the networking book, the answer didn't match.

The below is my code...

==============================================
import java.io.*;

public class BinaryDivision {
static String b_dividend, b_divisor, b_remainder, b_quotient; // in the binary form
static int d_dividend, d_divisor, d_remainder, d_quotient; // in the decimal form.

public static void main(String[] args) {
try {

//BufferedReader class is used to take the input from user.

System.out.println("Enter the binary value of your dividend:");

//The entered binary number is changed into decimal.
d_dividend = Integer.parseInt(b_dividend, 2);

System.out.println("Enter the binary value of your divisor:");

//The entered binary number is changed into decimal.
d_divisor = Integer.parseInt(b_divisor, 2);

//The dividend in the decimal form is printed.
System.out.println("d_dividend = " + d_dividend);

//The divisor in the decimal form is printed.
System.out.println("d_divisor = " + d_divisor);

//Remainder is calculated in decimal form.
d_remainder = d_dividend % d_divisor;

//The remainder is printed in decimal form.
System.out.println("d_remainder = " + d_remainder);

//quotient is calculated in decimal form.
d_quotient = d_dividend / d_divisor;

//quotient is printed in decimal form.
System.out.println("d_quotient = " + d_quotient);

//Remainder is converted into binary from decimal.
b_remainder = Integer.toBinaryString(d_remainder);

//quotient is converted into binary from decimal.
b_quotient = Integer.toBinaryString(d_quotient);

System.out.println("The remainder in the binary form is: "+ b_remainder);

System.out.println("The quotient in the binary form is: "+ b_quotient);
} catch (Exception e) {
e.printStackTrace();
}
}
}

=============================================
The below is my result..
=============================================

Enter the binary value of your dividend:
11010110110000
Enter the binary value of your divisor:
10011
d_dividend = 13744
d_divisor = 19
d_remainder = 7
d_quotient = 723
The remainder in the binary form is: 111
The quotient in the binary form is: 1011010011
==============================================
But the result should be somewhat like this....
==============================================

Enter the binary value of your dividend:
11010110110000
Enter the binary value of your divisor:
10011
d_dividend = 13744
d_divisor = 19
d_remainder = 7
d_quotient = 723
The remainder in the binary form is: 1110
The quotient in the binary form is: 1100001010
===============================================
can anyone tell me, where i went wrong in my code??
• 03-09-2011, 04:45 PM
couling
Please allways use code tags for representing code. This goes for every programming forum.

It appears that the only difference between the two is the binary representation (unless I've missed something).

Quote:

d_remainder = 7
d_quotient = 723
The remainder in the binary form is: 111
The quotient in the binary form is: 1011010011

These number are consistant (I havn't checked the maths but the binary's right).
111 = (1*4) + (1*2) + (1*1) = 7
Quote: