1. Converting binary numbers to decimal numbers

The basic method for converting a binary number to a decimal number is to first write the binary number as an expanded form with weighted coefficients, then sum according to the rules of decimal addition. This method is called theaddition by weightsmethod.

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


2. Converting decimal numbers to binary numbers

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

1. Converting decimal integers to binary integers

Converting a decimal integer to a binary integer uses thedivide by 2, take remainders, arrange in reverse ordermethod. The specific steps are: divide the decimal integer by 2 to get a quotient and a remainder; then divide the quotient by 2 again to get another quotient and remainder, and continue this process until the quotient becomes zero. Then take the first obtained remainder as the low-order significant bit of the binary number, and the later obtained remainders as the high-order significant bits, arranging them in sequence.

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

Solution:

2. Converting decimal fractions to binary fractions

Converting a decimal fraction to a binary fraction uses themultiply by 2, take the integer part, arrange in ordermethod. The specific steps are: multiply the decimal fraction by 2 to get a product; take out the integer part of the product; then multiply the remaining fractional part by 2 again to get another product; take out its integer part; continue this process until the fractional part of the product becomes zero, or until the required precision is reached.

Then arrange the extracted integer parts in order, with the first extracted integer as the high-order significant bit of the binary fraction, and the later extracted integers as the low-order significant bits.

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

Solution:

Example:

(173.8125)10=( )2

Solution:

在上个例子中得(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

Converting a decimal fraction to a binary fraction uses themultiply by 2, take the integer part, arrange in ordermethod. The specific steps are: multiply the decimal fraction by 2 to get a product; take out the integer part of the product; then multiply the remaining fractional part by 2 again to get another product; take out its integer part; 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 digit of the binary number. Or continue until the required precision is reached.

Then arrange the extracted integer parts in order, with the first extracted integer as the high-order significant bit of the binary fraction, and the later extracted integers as the low-order significant bits.

Decimal fraction to binary

Such as:0.625=(0.101)B

0.625*2=1.25======取出整数部分1 
0.25*2=0.5========取出整数部分0 
0.5*2=1==========取出整数部分1 

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

0.7*2=1.4========取出整数部分1 
0.4*2=0.8========取出整数部分0 
0.8*2=1.6========取出整数部分1 
0.6*2=1.2========取出整数部分1 
0.2*2=0.4========取出整数部分0  
0.4*2=0.8========取出整数部分0 
0.8*2=1.6========取出整数部分1 
0.6*2=1.2========取出整数部分1 
0.2*2=0.4========取出整数部分0

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