Perhaps middle school teachers will tell you how to convert decimal integers like 5, 10, or 20 into binary numbers, but has any teacher told you how to turn the decimal 0.1 into a binary fraction?
We convert a decimal integer to binary by following this procedure: within the rules of decimal calculation, divide the decimal number by 2 to get a quotient and a remainder; divide that quotient by 2 to get a new quotient and remainder; …repeat this until the quotient is 0. Then, arrange the remainders obtained each time in reverse order to get the binary number. For example, for 6:
\begin{aligned} 6 \div 2 &= 3 \dots 0 \\ 3 \div 2 &= 1 \dots 1 \\ 1 \div 2 &= 0 \dots 1 \end{aligned}
Reversing the order gives 110. This is the number 6 in binary.
Why does this operation work? This starts with the method of converting binary to decimal. The method to convert a binary number abcde to a decimal number is e + 2d + 2^2 c + 2^3 b + 2^4 a. This follows from binary counting rules: it essentially means that a, b, c, d, e represent the 10^4, 10^3, 10^2, 10^1, 10^0 positions respectively, but “10” in binary is 2 in decimal, which leads to the formula above. Once you understand the method for converting binary to decimal, it is not difficult to derive the rule for decimal to binary: by repeatedly dividing by 2 and calculating the remainders, you can find a, b, c, d, e respectively.
Integers are quite manageable, but what about decimals? For example, 0.1 = \frac{1}{10}, and since (10)_{10} = (1010)_2, we have: \left(\frac{1}{10}\right)_{10} = \left(\frac{1}{1010}\right)_2
If you are familiar with binary arithmetic, you can directly calculate \frac{1}{1010} within the binary system to find the binary representation of 0.1. However, this is a cumbersome method, primarily because most of us are not familiar with binary calculations. So what can we do? We can think of a roundabout way. Although we might not know the specific details of binary calculation, it is easy to see that dividing by “10” in binary is simple—just like in decimal, it involves moving the decimal point to the left. Therefore, we might as well first multiply the decimal by a power of 2, take the integer part, convert it to a binary integer, and then divide it by the binary power of 10.
For example, for 0.1: \begin{aligned} 0.1 \times 2^9 &= 51.2 \\ (51)_{10} &= (110011)_2 \\ 110011 \div 10^9 &= 0.000110011 \end{aligned}
This means that the decimal 0.1 is approximately 0.000110011 in binary. It is easy to see that this process is “never-ending,” meaning that the finite decimal 0.1 in base 10 is an infinite repeating decimal in binary! This is truly an interesting fact.
The computational load of the above method for converting decimals to binary is still a bit high. Therefore, it can be simplified into a lower-computation “multiply by 2 and take the integer” method:
Multiply the decimal by 2 to get an integer part and a fractional part. The integer part is the corresponding binary digit. Then multiply the resulting fractional part by 2 again to get a new integer part and fractional part. Repeat this process until the fractional part becomes 0 or the required precision is reached. The first integer obtained is the most significant bit (highest place value), and the last one obtained is the least significant bit.
For example, for 0.1:
0.1 \times 2 = 0.2, integer part 0,
fractional part 0.2;
0.2 \times 2 = 0.4, integer part 0,
fractional part 0.4;
0.4 \times 2 = 0.8, integer part 0,
fractional part 0.8;
0.8 \times 2 = 1.6, integer part 1,
fractional part 0.6;
0.6 \times 2 = 1.2, integer part 1,
fractional part 0.2;
…
It begins to cycle.
It is easy to obtain the binary representation as 0.000110011\dots, where the repeating period is 0011. In fact, most finite decimals in base 10 become infinite repeating decimals when converted to binary, except for those of the form \frac{1}{2^n}. Of course, regardless of the base, an irrational number in decimal will not become a rational number in binary, and vice versa.
When reposting, please include the original address of this article: https://kexue.fm/archives/1907
For more detailed information regarding reposting, please refer to: Scientific Space FAQ