# Decimal to Binary

• 11-17-2011, 03:45 PM
NetJava
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

{

NumDec = Integer.parseInt (line) ;
}

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

String NumBin = "";

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?
• 11-17-2011, 05:20 PM
eRaaaa
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 , .... ?
• 11-17-2011, 07:07 PM
Iron Lion
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.
• 11-17-2011, 09:14 PM
NetJava
Re: Decimal to Binary
Quote:

Originally Posted by eRaaaa
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?
• 11-18-2011, 03:37 PM
NetJava
Re: Decimal to Binary
Need it for today, up
• 11-18-2011, 03:43 PM
JosAH
Re: Decimal to Binary
Quote:

Originally Posted by NetJava
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