Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert Decimal to Binary No.  RSS feed

 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you're not allowed to use Integer.toBinaryString(int i) ?
 
Shafian Kisna
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
-----------------------------------------------------
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neat solution Campbell will remember that for future reference.

G
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already said, it's not my solution; I only remembered which book it is in.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!