Convert decimal to binary

I am currently reading the book "Code" by Charles Petzold. In it, he explains how to convert a decimal number to binary using the following pattern:

                [ ]   [ ]   [ ]   [ ]   [ ]   [ ]   [ ]   [ ]
                ÷128  ÷64   ÷32   ÷16   ÷8    ÷4    ÷2    ÷1
                [ ]   [ ]   [ ]   [ ]   [ ]   [ ]   [ ]   [ ]

      

The book explains how to use the template:

"Put a whole decimal number (less than or equal to 255) in the box in the upper left corner. Divide that number (dividend) by the first divisor (128) as indicated. Put the coefficient in the box below (box in the lower left corner) and the remainder in box on the right (second box in the top row) This first remainder is the dividend for the next calculation, which uses a divisor of 64. Continue in the same way through the template.

Keep in mind that each factor will be either 0 or 1. If the dividend is less than the divisor, then the factor is 0 and the remainder is just a dividend. if the dividend is greater than or equal to the divisor, then the coefficient is 1, and the remainder is a divisor - a divisor. Here's how it's done with 150: "

                [150]  [22]   [22]   [22]   [6 ]   [6 ]   [2 ]   [0 ]
                ÷128   ÷64    ÷32    ÷16    ÷8     ÷4     ÷2     ÷1
                [1 ]   [0 ]   [0 ]   [1 ]   [0 ]   [1 ]   [1 ]   [0 ]

      

But I'm puzzled! When I do the calculations according to the instructions, I get very different results. I am doing the following:

150 ÷ ​​128 = 1.171875 (I can't see where 22 appears at the top?) So I put 1 in the box below 150 and then I move 171875 and use that as a dividend for the next calculation, which of course leads me into all sorts of problems and ultimately not a binary number 10010110!

Can someone tell me where I am going wrong?

+2


source to share


5 answers


22 is the remainder of 150/128.

Since you have determined that there is 1,128 "in" 150, and given the value of this bit, you can forget about 128, which is "in" 150, so you take it off 150, leaving our 22. Then it's time for a digit worth 64 : 64, does not go to 22, so the digit is 0. And similarly for the digit equal to 32. Then for the digit worth 16: 16 goes 22 times, so there is 1 digit, and now you end up with 16 "in" 22 so take it back - leaving 6. And so on.



(Consider a similar case with base 10, say 309. Take column 100s, in 309 it is 3 100. So you put 3 there, and now 9 to the left. Then take column 10s, there are 0 10s in 9, so you put there 0 and then column 1s: 9 at 9, so you put 9. And now there is nothing left - you are done.)

I have a terrible feeling that may confuse more than clarify, but what am I thinking about it anyway.

+7


source


22 is the remainder.
150/128 = 1 remainder 22



+5


source


You need to do the integer

division.

// Floating point
150 ÷ 128 = 1.171875

// Integer
150 ÷ 128 = 1 remainder 22

      

So, you write it down 1

and carry it 22

to the next step.

+4


source


128 goes once per 150 with 22 remaining. Binary number 10010110 is converted to decimal;

150 = (1 * 128) + (1 * 16) + (1 * 4) * (1 + 2) = 128+16+4+2

      

In the same way, we could break the decimal number 150;

150 = (1 * 100) + (5 * 10) + (0 * 1) = 100 + 50

      

+2


source


This example uses integer operations and 150 - 128 => 22

.

The example is intentionally algebraic, however most modern languages ​​define bitwise binary operators. (Presumably, they will emulate if we ever build non-binary computers.) So it would be quite rare to do binary conversion in this way. More typically, you would use <<

, >>

and &

for direct detection of individual bits.

0


source







All Articles