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

I recently introduced the Decimal Number System, the one we are used as humans, and the Binary Number System, the one machines are used to.

我最近介绍了十进制数系统 (一种我们用作人类)和二进制数系统 (一种用于机器)。

In this tutorial I want to explain how to convert from decimal numbers to binary numbers.

在本教程中,我想解释如何从十进制数转换为二进制数。

We have a separate process for integers, and for fractions.

我们有一个单独的处理整数和分数的过程。

将整数从十进制转换为二进制 (Converting an integer from decimal to binary)

A decimal integer can be converted to binary by dividing it by 2.

十进制整数可以通过除以2转换为二进制。

Take the quotient, and keep dividing it by 2, until you reach zero.

取商,并将其除以2,直到达到零。

Each time you perform this division, take note of the remainder. Now reverse the remainders list, and you get the number in binary form.

每次执行该除法操作时,请注意余数 。 现在反转余数列表,您将获得二进制形式的数字。

Let’s make an example, I want to convert 29 into binary:

让我们举个例子,我想将29转换为二进制:

\[29\div2 = 14\] remainder 1

\ [29 \ div2 = 14 \]余数1

\[14\div2 = 7\] remainder 0

\ [14 \ div2 = 7 \]余数0

\[7\div2 = 3\] remainder 1

\ [7 \ div2 = 3 \]余数1

\[3\div2 = 1\] remainder 1

\ [3 \ div2 = 1 \]余数1

\[1\div2 = 0\] remainder 1

\ [1 \ div2 = 0 \]余数1

The binary number representing the 29 decimal is 11101.

代表29位十进制的二进制数是11101

Another example, let’s convert 145 decimal into binary.

另一个示例,让我们将145个十进制转换为二进制。

\[145\div2 = 72\] remainder 1

\ [145 \ div2 = 72 \]余数1

\[72\div2 = 36\] remainder 0

\ [72 \ div2 = 36 \]余数0

\[36\div2 = 18\] remainder 0

\ [36 \ div2 = 18 \]余数0

\[18\div2 = 9\] remainder 0

\ [18 \ div2 = 9 \]余数0

\[9\div2 = 4\] remainder 1

\ [9 \ div2 = 4 \]余数1

\[4\div2 = 2\] remainder 0

\ [4 \ div2 = 2 \]余数0

\[2\div2 = 1\] remainder 0

\ [2 \ div2 = 1 \]余数0

\[1\div2 = 0\] remainder 1

\ [1 \ div2 = 0 \]余数1

The binary number representing the 145 decimal is 10010001.

代表145个十进制的二进制数是10010001

将分数从十进制转换为二进制 (Converting a fraction from decimal to binary)

The decimal part of the fraction is converted separately like we did above. To convert the fractional part you need to multiply it by 2.

小数部分的小数部分将像上面一样单独转换。 要转换小数部分,您需要将其乘以2。

If the integer part of the fraction is still less than 1, assign it a 0. If it’s > 1, then assign it a 1, then keep multiplying by 2 and following this scheme.

如果分数的整数部分仍小于1 ,则将其分配为0 。 如果> 1 ,则为其分配1 ,然后继续乘以2并遵循此方案。

You stop when the fractional part is equal to 0.

当小数部分等于0时停止。

This might never happen, and you have a periodic fraction. In this case after some point you stop. The more digits the number has, in this case, the more precision it has.

这可能永远不会发生,并且您有周期性的分数。 在这种情况下,您要停下来。 在这种情况下,数字越多,其精度就越高。

Let’s make an example. I want to convert 0.375 to binary.

让我们举个例子。 我想将0.375转换为二进制。

\[0.375\times2 = 0.75 \implies 0\]

\ [0.375 \ times2 = 0.75 \ implies 0 \]

\[0.75\times2 = 1.5 \implies 1\]

\ [0.75 \ times2 = 1.5 \ implies 1 \]

\[0.5\times2 = 1 \implies 1\]

\ [0.5 \ times2 = 1 \表示1 \]

You take the number 0 or 1 that depends on being > 1, and you read it from top to bottom (instead of bottom to top like we do for the integer part). The final binary that translates .375 is 011.

您取数字01 (取决于> 1 ,然后从上到下阅读(而不是像我们对整数部分所做的那样从下至上)。 转换为.375的最终二进制文件是011

At this point you take the integer part (0) and the fractional part (011) separately, and you compose them.

此时,分别取整数部分( 0 )和小数部分( 011 ),然后将它们组成。

The number 0.375 converted to binary is 0.011

转换为二进制数的数值0.3750.011

翻译自: https://flaviocopes.com/converting-decimal-to-binary/

你可能感兴趣的