programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Convert Decimal to Binary No.

Ranch Hand
Posts: 70
Hi,

I am using this method to convert the decimal number, 75 to a binary no.

Could you pls help me? With the given algorithm, I am getting 1001001. But the actual answer is 1001011. I am uncertain as to why I am deriving to the value. Please show me if you have some algorithm that works.

Appreciate your speedy response.

Thanks!

Ranch Hand
Posts: 1325
what did the methods do in converting a decimal into binary? can you make it more clear or pasting your code here.

Suggestion:
you can use toBinaryString method of Integer class to convert into binary. see Java Doc here

hope it helps.

Sheriff
Posts: 14691
16
I suppose you're not allowed to use Integer.toBinaryString(int i) ?

Shafian Kisna
Ranch Hand
Posts: 70
Hi,

Yes I am no allowed to use any coding in here. Its working on Manual based.

I am using this algorithm:-
Now that we have an algorithm, we can use it to convert numbers from decimal to binary relatively painlessly. Let's try the number D=55.

* Our first step is to find P. We know that 2^4=16, 2^5=32, and 2^6=64. Therefore, P=5.
* 2^5<=55, so we put a 1 in the 2^5 column: 1-----.
* Subtracting 55-32 leaves us with 23. Subtracting 1 from P gives us 4.
* Following step 3 again, 2^4<=23, so we put a 1 in the 2^4 column: 11----.
* Next, subtract 16 from 23, to get 7. Subtract 1 from P gives us 3.
* 2^3>7, so we put a 0 in the 2^3 column: 110---
* Next, subtract 1 from P, which gives us 2.
* 2^2<=7, so we put a 1 in the 2^2 column: 1101--
* Subtract 4 from 7 to get 3. Subtract 1 from P to get 1.
* 2^1<=3, so we put a 1 in the 2^1 column: 11011-
* Subtract 2 from 3 to get 1. Subtract 1 from P to get 0.
* 2^0<=1, so we put a 1 in the 2^0 column: 110111
* Subtract 1 from 1 to get 0. Subtract 1 from P to get -1.
* P is now less than zero, so we stop.

Please try it with converting 75 to binary.

I am unable to get the right value.

Thanks!

Ranch Hand
Posts: 170
CODE
-------------------------------------------------------
int Actualnumber=75;
String result="";
String realresult="";
while (Actualnumber!=1)
{
int res=Actualnumber%2; //getting the remainder
Actualnumber=Actualnumber/2; //the remaining number
result=result+res; //concatenating to the result string
}
result=result+"1";
StringBuffer RealResult=new StringBuffer(result);
RealResult.reverse();//result will be in backwards so reverse the string
System.out.println("Result ="+RealResult);
-----------------------------------------------------

author
Sheriff
Posts: 23364
127
Manual doesn't work? Okay... here is 75, using your technique.

* First step is to find P. We know that 2^5=32, 2^6=64, and 2^7=128. Therefore, P=6.
* 2^6<=55, so we put a 1 in the 2^6 column: 01------.
* Subtracting 75-64 leaves us with 11.
* Next step is to find P. We know that 2^2=4, 2^3=8, and 2^4=16. Therefore, P=3.
* 2^3<=11, so we put a 1 in the 2^3 column: 01001---.
* Subtracting 11-8 leaves us with 3.
* Next step is to find P. We know that 2^0=1, 2^1=2, and 2^2=4. Therefore, P=1.
* 2^1<=3, so we put a 1 in the 2^1 column: 0100101-.
* Subtracting 3-2 leaves us with 1.
* Next step is to find P. We know that 2^0=1. Therefore, P=0.
* 2^0<=1, so we put a 1 in the 2^0 column: 01001011.
* Subtracting 1-1 leaves us with 0.

Henry
[ April 27, 2007: Message edited by: Henry Wong ]

(instanceof Sidekick)
Ranch Hand
Posts: 8791
I did it like this once in COBOL. This isn't COBOL or anything that will compile and run. And I think it's exactly what Henry said ...

I made factor big enough for any input and it gives a fixed size output. If you want to be more dynamic you can compute factor as the largest power of base that is not larger than input. Then you won't have any leading zeros.
[ April 27, 2007: Message edited by: Stan James ]

Marshal
Posts: 57437
175
The manual technique shouldn't need any calculation of P. This following example comes from Alan Clements, the Principles of Computer Hardware 3/e, Oxford: Oxford University Press (2000). On page 156 it has an example for 123. For 75:-You can keep dividing 0 / 2 as often as you wish to start the result with 00000000000000000000000.

Read the remainders backwards, from bottom to top, and you get 01001011.

QED
[ April 27, 2007: Message edited by: Campbell Ritchie ]

Ranch Hand
Posts: 1296
you should note also that the techniques described work with any number base

Base-16

Base-8

[ April 27, 2007: Message edited by: Garrett Rowe ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Nice, Campbell. I knew I had done it right to left at some point in life but couldn't get it quite right on paper this morning.

Campbell Ritchie
Marshal
Posts: 57437
175
Pleased to help, Stan, and Shafian, and everybody else. But I didn't remember how to do it. I only remembered which book I would find it in . . .

Shafian Kisna
Ranch Hand
Posts: 70
Hi Guys,

Thanks for the suggestion...I will work along with Campbell's method as that is the one is simple to understand....

Was looking for something like that...

Once again, appreciate your help!

Thanks!

Ranch Hand
Posts: 333
neat solution Campbell will remember that for future reference.

G

Campbell Ritchie
Marshal
Posts: 57437
175
I've already said, it's not my solution; I only remembered which book it is in.

 If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS