Results 1 to 2 of 2
  1. #1
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default 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??

  2. #2
    couling is offline Member
    Join Date
    Nov 2010
    Posts
    54
    Rep Power
    0

    Default

    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 Originally Posted by amityadav9314 View Post
    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 Originally Posted by amityadav9314 View Post
    d_remainder = 7
    d_quotient = 723
    The remainder in the binary form is: 1110
    The quotient in the binary form is: 1100001010
    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

  1. ASCII to binary code
    By Vagabond.drv in forum New To Java
    Replies: 12
    Last Post: 01-13-2011, 01:52 PM
  2. code for binary tree in jsp
    By ajay kumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-17-2009, 06:45 AM
  3. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 04:57 PM
  4. Replies: 3
    Last Post: 04-14-2009, 04:44 PM
  5. need a code to get the result of avgscan.exe file
    By Michael in forum New To Java
    Replies: 0
    Last Post: 06-11-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •