Results 1 to 2 of 2
 03092011, 02:15 PM #1Member
 Join Date
 Sep 2010
 Posts
 10
 Rep Power
 0
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.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the binary value of your dividend:");
b_dividend = br.readLine();
//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:");
b_divisor = br.readLine();
//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??
 03092011, 04:45 PM #2Member
 Join Date
 Nov 2010
 Posts
 54
 Rep Power
 0
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).
These number are consistant (I havn't checked the maths but the binary's right).
111 = (1*4) + (1*2) + (1*1) = 7
These numbers are not consistant. If the book has printed this then the book's got a misprint.
1110 = (1*8) + (1*4) + (1*2) + (0*1) = 14
Note:
If you use windows, then windows calculator can do binary to decimal conversion. Click view>scientific then tap in your number and switch between "Dec" and "Bin".Signature 
Please use [CODE] tags and indent correctly. It really helps when reading your code.
Similar Threads

ASCII to binary code
By Vagabond.drv in forum New To JavaReplies: 12Last Post: 01132011, 02:52 PM 
code for binary tree in jsp
By ajay kumar in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 11172009, 07:45 AM 
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10072009, 04:57 PM 
Debug gives same result when I change the code!
By RedSix in forum NetBeansReplies: 3Last Post: 04142009, 04:44 PM 
need a code to get the result of avgscan.exe file
By Michael in forum New To JavaReplies: 0Last Post: 06112008, 11:43 AM
Bookmarks