Results 1 to 6 of 6
Thread: Decimal to Binary
- 11-17-2011, 02:45 PM #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
{
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?
- 11-17-2011, 04:20 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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, 06:07 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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, 08:14 PM #4
Member
- 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?
- 11-18-2011, 02:37 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
Re: Decimal to Binary
Need it for today, up
- 11-18-2011, 02:43 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Decimal to Binary
By DMarsh12 in forum New To JavaReplies: 4Last Post: 10-01-2011, 06:24 PM -
anyone know's how to program a conversion of binary-decimal , decimal-binary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08-25-2011, 07:32 PM -
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12-26-2010, 09:59 AM -
Convert Decimal To Binary
By aspire007 in forum New To JavaReplies: 8Last Post: 08-06-2010, 07:32 AM -
Binary to Decimal Converter
By c_walker in forum New To JavaReplies: 15Last Post: 11-24-2009, 02:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks