Decimal
To convert from a base-10 integer numeral to its base-2 (binary) equivalent, the number is divided by two, and the remainder is the least-significant bit. The (integer) result is again divided by two, its remainder is the next most significant bit. This process repeats until the result of further division becomes zero.
For example, 11810, in binary, is:
Operation Remainder
118 ÷ 2 = 59 0
59 ÷ 2 = 29 1
29 ÷ 2 = 14 1
14 ÷ 2 = 7 0
7 ÷ 2 = 3 1
3 ÷ 2 = 1 1
1 ÷ 2 = 0 1
Reading the sequence of remainders from the bottom up gives the binary numeral 11101102.
This method works for conversion from any base, but there are better methods for bases which are powers of two, such as octal and hexadecimal given below.
To convert from base-2 to base-10 is the reverse algorithm. Starting from the left, double the result and add the next digit until there are no more. For example to convert 1100101011012 to decimal:
Result Remaining digits
0 110010101101
0 × 2 + 1 = 1 10010101101
1 × 2 + 1 = 3 0010101101
3 × 2 + 0 = 6 010101101
6 × 2 + 0 = 12 10101101
12 × 2 + 1 = 25 0101101
25 × 2 + 0 = 50 101101
50 × 2 + 1 = 101 01101
101 × 2 + 0 = 202 1101
202 × 2 + 1 = 405 101
405 × 2 + 1 = 811 01
811 × 2 + 0 = 1622 1
1622 × 2 + 1 = 3245
The result is 324510.
The fractional parts of a numbers are converted with similar methods. They are again based on the equivalence of shifting with doubling or halving.
In a fractional binary number such as .110101101012, the first digit is , the second , etc. So if there is a 1 in the first place after the decimal, then the number is at least , and vice versa. Double that number is at least 1. This suggests the algorithm: Repeatedly double the number to be converted, record if the result is at least 1, and then throw away the integer part.
For example, 10, in binary, is:
Converting Result
0.
0.0
0.01
0.010
0.0101
Thus the repeating decimal fraction 0.3... is equivalent to the repeating binary fraction 0.01... .
Or for example, 0.110, in binary, is:
Converting Result
0.1 0.
0.1 × 2 = 0.2 < 1 0.0
0.2 × 2 = 0.4 < 1 0.00
0.4 × 2 = 0.8 < 1 0.000
0.8 × 2 = 1.6 ≥ 1 0.0001
0.6 × 2 = 1.2 ≥ 1 0.00011
0.2 × 2 = 0.4 < 1 0.000110
0.4 × 2 = 0.8 < 1 0.0001100
0.8 × 2 = 1.6 ≥ 1 0.00011001
0.6 × 2 = 1.2 ≥ 1 0.000110011
0.2 × 2 = 0.4 < 1 0.0001100110
This is also a repeating binary fraction 0.000110011... . It may come as a surprise that terminating decimal fractions can have repeating expansions in binary. It is for this reason that many are surprised to discover that 0.1 + ... + 0.1, (10 additions) differs from 1 in floating point arithmetic. In fact, the only binary fractions with terminating expansions are of the form of an integer divided by a power of 2, which 1/10 is not.
The final conversion is from binary to decimal fractions. The only difficulty arises with repeating fractions, but otherwise the method is to shift the fraction to an integer, convert it as above, and then divide by the appropriate power of two in the decimal base. For example:
x = 1100 .101110011100...
= 1100101110 .0111001110...
= 11001 .0111001110...
= 1100010101
x = (789/62)10
Another way of converting from binary to decimal, often quicker for a person familiar with hexadecimal, is to do so indirectly—first converting (x in binary) into (x in hexadecimal) and then converting (x in hexadecimal) into (x in decimal).
2007-03-27 20:12:26
·
answer #1
·
answered by necromancer 3
·
0⤊
0⤋