Easy Tutorial
❮ Android Tutorial Json Android Tutorial Project Src Analysis ❯

Decimal Fractions Converted into Binary Fractions

Category Programming Techniques

I. Converting Binary Numbers to Decimal Numbers

The basic method for converting a binary number to a decimal number is to first express the binary number in terms of weighted coefficients, and then sum them according to the rules of decimal addition. This method is called the "weighted addition" method.

For example, converting the binary number 110.11 to a decimal number.


II. Converting Decimal Numbers to Binary Numbers

When converting a decimal number to a binary number, since the conversion methods for integers and fractions are different, first convert the integer and fractional parts of the decimal number separately, and then combine them.

1. Converting Decimal Integers to Binary Integers

To convert a decimal integer to a binary integer, the "divide by 2 and take the remainder, then arrange in reverse order" method is used. The specific procedure is: divide the decimal integer by 2 to get a quotient and a remainder; then divide the quotient by 2 again, and you will get another quotient and remainder, continue this process until the quotient is zero, then arrange the remainders in the order you obtained them, with the first remainder as the least significant bit of the binary number and the last remainder as the most significant bit.

For example, converting (173)10 to a binary number.

Solution:


2. Converting Decimal Fractions to Binary Fractions

To convert a decimal fraction to a binary fraction, the "multiply by 2 and take the integer part, then arrange in order" method is used. The specific procedure is: multiply the decimal fraction by 2 to get a product, take the integer part of the product, then multiply the remaining fractional part by 2 to get another product, take the integer part of this product, continue this process until the fractional part of the product is zero, or until the required precision is reached.

Then arrange the integer parts taken in order, with the first integer as the most significant bit of the binary fraction and the last integer as the least significant bit.

For example, converting (0.8125) to a binary fraction.

Solution:

Example:

(173.8125)10 = ( )2

Solution:

In the previous example, (173)10 = (10101101)2
(0.8125)10 = (0.1101)2

Combine the integer part and the fractional part to get:

(173.8125)10 = (10101101.1101)2

The conversion of decimal fractions to binary fractions uses the "multiply by 2 and take the integer part, then arrange in order" method. The specific procedure is: multiply the decimal fraction by 2 to get a product, take the integer part of the product, then multiply the remaining fractional part by 2 to get another product, take the integer part of this product, continue this process until the integer part of the product is zero, or the integer part is 1, at which point 0 or 1 is the last bit of the binary number. Or until the required precision is reached.

Then arrange the integer parts taken in order, with the first integer as the most significant bit of the binary fraction and the last integer as the least significant bit.

Decimal Fraction to Binary

For example: 0.625 = (0.101)B

0.625 * 2 = 1.25======Take the integer part 1
0.25 * 2 = 0.5========Take the integer part 0
0.5 * 2 = 1==========Take the integer part 1

Another example: 0.7 = (0.1 0110 0110...)B

0.7 * 2 = 1.4========Take the integer part 1
0.4 * 2 = 0.8========Take the integer part 0
0.8 * 2 = 1.6========Take the integer part 1
0.6 * 2 = 1.2========Take the integer part 1
0.2 * 2 = 0.4========Take the integer part 0
0.4 * 2 = 0.8========Take the integer part 0
0.8 * 2 = 1.6========Take the integer part 1
0.6 * 2 = 1.2========Take the integer part 1
0.2 * 2 = 0.4========Take the integer part 0

>

Original article link: https://www.cnblogs.com/xkfz007/articles/2590472.html

** Click to share notes

[Cancel

❮ Android Tutorial Json Android Tutorial Project Src Analysis ❯