1. Member Join Date
Nov 2011
Posts
7
Rep Power
0

## 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?  Reply With Quote

2. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
12

## 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 , .... ?  Reply With Quote

3. Senior Member Join Date
Nov 2010
Posts
210
Rep Power
11

## 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.  Reply With Quote

4. Member Join Date
Nov 2011
Posts
7
Rep Power
0

## Re: Decimal to Binary 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?  Reply With Quote

5. Member Join Date
Nov 2011
Posts
7
Rep Power
0

## Re: Decimal to Binary

Need it for today, up  Reply With Quote

6. ## Re: Decimal to Binary 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  Reply With Quote

#### Posting Permissions

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