Results 1 to 6 of 6
  1. #1
    NetJava is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Decimal to Binary

    Hi all,
    I have this problem: Transform a Decimal to a Binary.
    So, i did this:

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    public static void main(String[] args) throws IOException {
    int NumDec;
    int R;

    System.out.println("Decimal");
    do

    {

    BufferedReader input = new BufferedReader ( new InputStreamReader (System.in));
    String line = input.readLine();

    NumDec = Integer.parseInt (line) ;
    }

    while((NumDec <= 0) || (NumDec != NumDec));

    String NumBin = "";

    BufferedReader input = new BufferedReader ( new InputStreamReader (System.in));

    while(NumDec != 0)
    {
    NumDec = NumDec/2;
    R = NumDec%2;
    NumBin = NumBin+R;
    }

    System.out.println("Binary: "+NumBin);
    }
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    Well, the problem is ouput.

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    Decimal
    2
    Binary: 10

    Decimal
    8
    Binary: 0010

    Decimal
    9
    Binary: 0010

    Decimal
    55
    Binary: 110110

    ...so on...
    -------------------------------------------------------------------------------------------------------------------------------------------------------------


    Can you help me?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    7

    Default Re: Decimal to Binary

    First, your solution/algorithm is wrong :)
    while (NumDec != 0) {
    NumDec = NumDec / 2;
    R = NumDec % 2;
    NumBin = NumBin + R;
    }
    % 2 is not the correct way. You have to calculate the rest of NumDec/2 to the prevoius NumDec
    8 / 2 = 4 --> 4*2 = 8 - 8 = 0
    4 / 2 = 2 --> 2*2 = 4 - 4 = 0
    2 / 2 = 1 --> 1*2 = 2 -2 = 0
    1/ 2 = 0 --> 0*2 = 1 - 0 = 1

    in other words, other example (11 to binary):
    11 : 2 = 5 Rest: 1 (5*2 = 10) -> 11 - 10
    5 : 2 = 2 Rest: 1 (2*2 = 4) -> 5 - 4
    2 : 2 = 1 Rest: 0 (1*2 = 2) -> 2 -2
    1 : 2 = 0 Rest: 1 (0*2 = 0) -> 1 - 0

    Second: NumDec != NumDec makes no sense, why a second BufferedReader , .... ?

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    6

    Default Re: Decimal to Binary

    Bit-shifting is useful here. Hint: to see if the ith bit in an int (where 31 is the leftmost bit and 0 the rightmost) is set, use (num & (1 << i)) != 0.

  4. #4
    NetJava is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Decimal to Binary

    Quote Originally Posted by eRaaaa View Post
    First, your solution/algorithm is wrong :)
    while (NumDec != 0) {
    NumDec = NumDec / 2;
    R = NumDec % 2;
    NumBin = NumBin + R;
    }
    % 2 is not the correct way. You have to calculate the rest of NumDec/2 to the prevoius NumDec
    8 / 2 = 4 --> 4*2 = 8 - 8 = 0
    4 / 2 = 2 --> 2*2 = 4 - 4 = 0
    2 / 2 = 1 --> 1*2 = 2 -2 = 0
    1/ 2 = 0 --> 0*2 = 1 - 0 = 1

    in other words, other example (11 to binary):
    11 : 2 = 5 Rest: 1 (5*2 = 10) -> 11 - 10
    5 : 2 = 2 Rest: 1 (2*2 = 4) -> 5 - 4
    2 : 2 = 1 Rest: 0 (1*2 = 2) -> 2 -2
    1 : 2 = 0 Rest: 1 (0*2 = 0) -> 1 - 0

    Second: NumDec != NumDec makes no sense, why a second BufferedReader , .... ?
    Second BufferedReader because I copied the structure of the iteration pre Conditional :P

    Next: I know that NumDec != NumDec is a no sense. The condition is the number != int(number) so check that a number is an integer and not a float , but i didn't know how to write

    How can i write (compile) the Rest?

  5. #5
    NetJava is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Decimal to Binary

    Need it for today, up

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,188
    Blog Entries
    7
    Rep Power
    24

    Default Re: Decimal to Binary

    Quote Originally Posted by NetJava View Post
    Need it for today, up
    Well, what are you waiting for? You got all the hints and suggestions you need; we are not going to do your homework for you.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Decimal to Binary
    By DMarsh12 in forum New To Java
    Replies: 4
    Last Post: 10-01-2011, 06:24 PM
  2. Replies: 5
    Last Post: 08-25-2011, 07:32 PM
  3. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 10:59 AM
  4. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  5. Binary to Decimal Converter
    By c_walker in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 03:38 PM

Posting Permissions

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