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!
==Conversion to and from other numeral systems== ===Decimal to binary=== [[File:Decimal to Binary Conversion.gif|alt=|frame|Conversion of (357)<sub>10</sub> to binary notation results in (101100101)]] To convert from a base-10 [[Integer (computer science)|integer]] to its base-2 (binary) equivalent, the number is [[division by two|divided by two]]. The remainder is the [[least-significant bit]]. The quotient is again divided by two; its remainder becomes the next least significant bit. This process repeats until a quotient of one is reached. The sequence of remainders (including the final quotient of one) forms the binary value, as each remainder must be either zero or one when dividing by two. For example, (357)<sub>10</sub> is expressed as (101100101)<sub>2.</sub><ref>{{Cite web|url=https://www.chalkstreet.com/aptipedia/knowledgebase/base-system/|title=Base System|access-date=31 August 2016|archive-date=23 October 2017|archive-url=https://web.archive.org/web/20171023010521/https://www.chalkstreet.com/aptipedia/knowledgebase/base-system/|url-status=usurped}}</ref> === 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. ===Hexadecimal=== {{Main|Hexadecimal}} {{Hexadecimal table}} Binary may be converted to and from hexadecimal more easily. This is because the [[radix]] of the hexadecimal system (16) is a power of the radix of the binary system (2). More specifically, 16 = 2<sup>4</sup>, so it takes four digits of binary to represent one digit of hexadecimal, as shown in the adjacent table. To convert a hexadecimal number into its binary equivalent, simply substitute the corresponding binary digits: :3A<sub>16</sub> = 0011 1010<sub>2</sub> :E7<sub>16</sub> = 1110 0111<sub>2</sub> To convert a binary number into its hexadecimal equivalent, divide it into groups of four bits. If the number of bits isn't a multiple of four, simply insert extra '''0''' bits at the left (called [[Padding (cryptography)#Bit padding|padding]]). For example: :1010010<sub>2</sub> = 0101 0010 grouped with padding = 52<sub>16</sub> :11011101<sub>2</sub> = 1101 1101 grouped = DD<sub>16</sub> To convert a hexadecimal number into its decimal equivalent, multiply the decimal equivalent of each hexadecimal digit by the corresponding power of 16 and add the resulting values: :C0E7<sub>16</sub> = (12 × 16<sup>3</sup>) + (0 × 16<sup>2</sup>) + (14 × 16<sup>1</sup>) + (7 × 16<sup>0</sup>) = (12 × 4096) + (0 × 256) + (14 × 16) + (7 × 1) = 49,383<sub>10</sub> ===Octal=== {{Main|Octal}} Binary is also easily converted to the [[octal]] numeral system, since octal uses a radix of 8, which is a [[power of two]] (namely, 2<sup>3</sup>, so it takes exactly three binary digits to represent an octal digit). The correspondence between octal and binary numerals is the same as for the first eight digits of [[hexadecimal]] in the table above. Binary 000 is equivalent to the octal digit 0, binary 111 is equivalent to octal 7, and so forth. {| class="wikitable" style="text-align:center" !Octal!!Binary |- | 0 || 000 |- | 1 || 001 |- | 2 || 010 |- | 3 || 011 |- | 4 || 100 |- | 5 || 101 |- | 6 || 110 |- | 7 || 111 |} Converting from octal to binary proceeds in the same fashion as it does for [[hexadecimal]]: :65<sub>8</sub> = 110 101<sub>2</sub> :17<sub>8</sub> = 001 111<sub>2</sub> And from binary to octal: :101100<sub>2</sub> = 101 100<sub>2</sub> grouped = 54<sub>8</sub> :10011<sub>2</sub> = 010 011<sub>2</sub> grouped with padding = 23<sub>8</sub> And from octal to decimal: :65<sub>8</sub> = (6 × 8<sup>1</sup>) + (5 × 8<sup>0</sup>) = (6 × 8) + (5 × 1) = 53<sub>10</sub> :127<sub>8</sub> = (1 × 8<sup>2</sup>) + (2 × 8<sup>1</sup>) + (7 × 8<sup>0</sup>) = (1 × 64) + (2 × 8) + (7 × 1) = 87<sub>10</sub>
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)