Results 1 to 6 of 6
Thread: Decimal to Binary
 11172011, 03:45 PM #1Member
 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
{
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?
 11172011, 05:20 PM #2Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 7
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 , .... ?
 11172011, 07:07 PM #3Senior Member
 Join Date
 Nov 2010
 Posts
 210
 Rep Power
 6
Re: Decimal to Binary
Bitshifting 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.
 11172011, 09:14 PM #4Member
 Join Date
 Nov 2011
 Posts
 7
 Rep Power
 0
Re: Decimal to Binary
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?
 11182011, 03:37 PM #5Member
 Join Date
 Nov 2011
 Posts
 7
 Rep Power
 0
Re: Decimal to Binary
Need it for today, up
 11182011, 03:43 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,292
 Blog Entries
 7
 Rep Power
 24
Similar Threads

Decimal to Binary
By DMarsh12 in forum New To JavaReplies: 4Last Post: 10012011, 06:24 PM 
anyone know's how to program a conversion of binarydecimal , decimalbinary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08252011, 07:32 PM 
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12262010, 10:59 AM 
Convert Decimal To Binary
By aspire007 in forum New To JavaReplies: 8Last Post: 08062010, 07:32 AM 
Binary to Decimal Converter
By c_walker in forum New To JavaReplies: 15Last Post: 11242009, 03:38 PM
Bookmarks