Converting a decimal number into binary
I am currently reading Charles Petzold's book开发者_如何学Python 'Code'. In it he explains how to convert a decimal number into binary using the following template:
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
÷128 ÷64 ÷32 ÷16 ÷8 ÷4 ÷2 ÷1
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
In the book, his explanation on how to use the template reads:
"Put the entire decimal number (less than or equal to 255) in the box in the upper left corner. Divide that number (the dividend) by the first divisor (128), as indicated. Put the quotient in the box below (the box at the lower left corner), and the remainder in the box to the right (the second box on the top row). That first remainder is the dividend for the next calculation, which uses a divisor of 64. Continue in the same manner through the template.
Keep in mind that each quotient will be either 0 or 1. If the dividend is less than the divisor the quotient is 0 and the remainder is simply the dividend. if the dividend is greater than or equal to the divisor, the quotient is 1 and the remainder is the divider - the 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 as instructed, I'm getting very different results. What I'm doing is as follows:
150 ÷ 128 = 1.171875 (I don't see where the 22 comes from above?) So, I place a 1 in the box below the 150 and then carry the 171875 and use that as the dividend for the next calculation, which of course gets me into all sorts of problems, and ultimately, not the binary number 10010110!
Can somebody tell me where I'm going wrong?
22 is the remainder of 150/128.
Since you've determined that there's 1 128 "in" 150, and given a value to that bit, you can forget about the 128 that's "in" 150, so you take it away from 150, leaving our 22. Then it's time for the digit worth 64: 64 doesn't go into 22, so that digit is a 0. And similarly for the digit worth 32. Then for the digit worth 16: 16 goes into 22 once, so there's a 1 digit there, and now you're done with the 16 "in" 22, so take it away -- leaving 6. And so on.
(Consider a similar base 10 case, let's say 309. Take the 100s column; there are 3 100s in 309, so you put a 3 there. And now there's 9 left over. Then take the 10s column; there are 0 10s in 9, so you put a 0 there. And then the 1s column: there are 9 1s in 9, so you put a 9 in there. And now there's nothing left -- you're done.)
I have a horrible feeling this might confuse more than clarify, but that's how I think of it anyway.
The 22 is the remainder.
150/128 = 1 remainder 22
You need to do integer
division.
// Floating point
150 ÷ 128 = 1.171875
// Integer
150 ÷ 128 = 1 remainder 22
So, you write down the 1
and carry 22
to the next step.
128 goes once into 150 with 22 remaining. The binary number 10010110 translates to the decimal;
150 = (1 * 128) + (1 * 16) + (1 * 4) * (1 + 2) = 128+16+4+2
In the same way we could break down the decimal number 150;
150 = (1 * 100) + (5 * 10) + (0 * 1) = 100 + 50
The example uses integer operations, and 150 - 128 => 22
.
The example is deliberately algebraic, however most modern languages define bitwise binary operators. (Presumably these will be emulated if we ever build non-binary computers.) So it would be quite rare to actually do binary conversion that way. More typically you would use <<
, >>
, and &
to detect individual bits directly.
精彩评论