Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Binary number
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Binary to decimal === Conversion from base-2 to base-10 simply inverts the preceding algorithm. The bits of the binary number are used one by one, starting with the most significant (leftmost) bit. Beginning with the value 0, the prior value is doubled, and the next bit is then added to produce the next value. This can be organized in a multi-column table. For example, to convert 10010101101<sub>2</sub> to decimal: {| style= "border: 1px solid #a2a9b1; border-spacing: 3px; background-color: #f8f9fa; color: black; margin: 0.5em 0 0.5em 1em; padding: 0.2em; line-height: 1.5em; width:22em" !Prior value ! style="text-align:left" | × 2 + !Next bit != Next value |- |align="right"|0 ||× 2 +|| '''1''' || = 1 |- |align="right"|1 ||× 2 +|| '''0''' || = 2 |- |align="right"|2 ||× 2 +|| '''0''' || = 4 |- |align="right"|4 ||× 2 +|| '''1''' || = 9 |- |align="right"|9 ||× 2 +|| '''0''' || = 18 |- |align="right"|18 ||× 2 +|| '''1''' || = 37 |- |align="right"|37 ||× 2 +|| '''0''' || = 74 |- |align="right"|74 ||× 2 +|| '''1''' || = 149 |- |align="right"|149 ||× 2 +|| '''1''' || = 299 |- |align="right"|299 ||× 2 +|| '''0''' || = 598 |- |align="right"|598 ||× 2 +|| '''1''' || = '''1197''' |} The result is 1197<sub>10</sub>. The first Prior Value of 0 is simply an initial decimal value. This method is an application of the [[Horner scheme]]. {| ! Binary | 1 || 0 || 0 || 1 || 0 || 1 || 0 || 1 || 1 || 0 || 1 || |- ! Decimal | 1×2<sup>10</sup> + || 0×2<sup>9</sup> + || 0×2<sup>8</sup> + || 1×2<sup>7</sup> + || 0×2<sup>6</sup> + || 1×2<sup>5</sup> + || 0×2<sup>4</sup> + || 1×2<sup>3</sup> + || 1×2<sup>2</sup> + || 0×2<sup>1</sup> + || 1×2<sup>0</sup> = || 1197 |} The fractional parts of a number 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 0.11010110101<sub>2</sub>, the first digit is <math display="inline">\frac{1}{2} </math>, the second <math display="inline"> (\frac{1}{2})^2 = \frac{1}{4} </math>, etc. So if there is a 1 in the first place after the decimal, then the number is at least <math display="inline"> \frac{1}{2} </math>, 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, <math display="inline"> (\frac{1}{3})_{10} </math>, in binary, is: {| class="wikitable" !Converting!!Result |- |<math display="inline"> \frac{1}{3} </math> || 0. |- |<math display="inline"> \frac{1}{3} \times 2 = \frac{2}{3} < 1 </math> || 0.0 |- |<math display="inline"> \frac{2}{3} \times 2 = 1\frac{1}{3} \ge 1</math> || 0.01 |- |<math display="inline"> \frac{1}{3} \times 2 = \frac{2}{3} < 1 </math> || 0.010 |- |<math display="inline"> \frac{2}{3} \times 2 = 1\frac{1}{3} \ge 1 </math> || 0.0101 |} Thus the repeating decimal fraction 0.{{overline|3}}... is equivalent to the repeating binary fraction 0.{{overline|01}}... . Or for example, 0.1<sub>10</sub>, in binary, is: {| class="wikitable" ! 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.0{{overline|0011}}... . 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 1/10 + ... + 1/10 (addition of 10 numbers) differs from 1 in binary [[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: <math display="block">\begin{align} x & = & 1100&.1\overline{01110}\ldots \\ x\times 2^6 & = & 1100101110&.\overline{01110}\ldots \\ x\times 2 & = & 11001&.\overline{01110}\ldots \\ x\times(2^6-2) & = & 1100010101 \\ x & = & 1100010101/111110 \\ x & = & (789/62)_{10} \end{align}</math> Another way of converting from binary to decimal, often quicker for a person familiar with [[hexadecimal]], is to do so indirectly—first converting (<math>x</math> in binary) into (<math>x</math> in hexadecimal) and then converting (<math>x</math> in hexadecimal) into (<math>x</math> in decimal). For very large numbers, these simple methods are inefficient because they perform a large number of multiplications or divisions where one operand is very large. A simple divide-and-conquer algorithm is more effective asymptotically: given a binary number, it is divided by 10<sup>''k''</sup>, where ''k'' is chosen so that the quotient roughly equals the remainder; then each of these pieces is converted to decimal and the two are [[Concatenation|concatenated]]. Given a decimal number, it can be split into two pieces of about the same size, each of which is converted to binary, whereupon the first converted piece is multiplied by 10<sup>''k''</sup> and added to the second converted piece, where ''k'' is the number of decimal digits in the second, least-significant piece before conversion.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)