简体   繁体   English

将十进制数转换为二进制数

[英]Converting a decimal number into binary

I am currently reading Charles Petzold's book 'Code'. 我目前正在阅读Charles Petzold的书“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. “将整个十进制数字(小于或等于255)放在左上角的方框中。将该数字(被除数)除以第一个除数(128),如下所示。将商放在下面的方框中(左下角的框,以及右边框中的剩余部分(顶行的第二个框)。第一个余数是下一次计算的红利,它使用64的除数。以相同的方式继续通过模板。

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. 请记住,每个商将为0或1.如果被除数小于除数,则商为0,余数仅为被除数。 if the dividend is greater than or equal to the divisor, the quotient is 1 and the remainder is the divider - the divisor. 如果被除数大于或等于除数,则商为1,余数为除数 - 除数。 Here's how it's done with 150:" 这是用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! 150÷128 = 1.171875 (我看不到22从哪里来?)所以,我在150下面的方框中放置一个1,然后携带171875并将其用作下一次计算的红利,当然让我陷入各种各样的问题,最终,不是二进制数10010110!

Can somebody tell me where I'm going wrong? 谁能告诉我哪里出错了?

22 is the remainder of 150/128. 22是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. 既然你已经确定有1 128“in”150,并给出了该位的值,那么你可以忘记那个“在”150中的128,所以你把它从150那里拿走,离开我们的22.然后是时候了64:64的数字不会进入22,所以该数字为0.而且数字值为32.然后数字值16:16进入22,因此那里有1位数,现在你完成了16“in”22,所以把它带走 - 留下6.依此类推。

(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.) (考虑一个类似的基础10的情况,比方说309.拿100s列; 309中有3 100个,所以你在那里放了3个。现在剩下9个。然后取10s列; 9个中有0个10个,所以你把0放在那里。然后是1s栏:9中有9个1,所以你把9放在那里。现在没有什么了 - 你已经完成了。)

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. 剩下的就是22。
150/128 = 1 remainder 22 150/128 = 1余数22

You need to do integer division. 你需要做integer除法。

// 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. 所以,你写下1并将22带到下一步。

128 goes once into 150 with 22 remaining. 128进入150,其余22。 The binary number 10010110 translates to the decimal; 二进制数10010110转换为小数;

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;

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

The example uses integer operations, and 150 - 128 => 22 . 该示例使用整数运算,并且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. 更典型的是,您将使用<<>>&来直接检测各个位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM