Shafian Kisna

Ranch Hand

Posts: 70

posted 10 years ago

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!

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!

posted 10 years ago

I suppose you're not allowed to use Integer.toBinaryString(int i) ?

[My Blog]
*All roads lead to JavaRanch*

Shafian Kisna

Ranch Hand

Posts: 70

posted 10 years ago

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!

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

posted 10 years ago

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);

-----------------------------------------------------

-------------------------------------------------------

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);

-----------------------------------------------------

posted 10 years ago

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 ]

* 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

Ranch Hand

Posts: 8791

posted 10 years ago

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 ]

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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Campbell Ritchie

Marshal

Posts: 55680

161

posted 10 years ago

The manual technique shouldn't need any calculation of P. This following example comes from Alan Clements,

Read the remainders

QED

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

*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

posted 10 years ago

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 ]

Base-16

Base-8

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

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 10 years ago
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

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

Shafian Kisna

Ranch Hand

Posts: 70

Gavin Tranter

Ranch Hand

Posts: 333

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |