Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C

Transform between IEEE, IBM or VAX floating point number formats and bytes expressions

Rate me:
Please Sign up or sign in to vote.
4.74/5 (14 votes)
21 Nov 20056 min read 106.2K   1.9K   27  
This program can transform between IEEE, IBM or VAX floating point number formats and their bytes expressions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="KEYWORDS" content="Binary numeral system">
<meta name="robots" content="index,follow">
<link rel="shortcut icon" href="http://en.wikipedia.org/favicon.ico">
<link rel="copyright" href="http://www.gnu.org/copyleft/fdl.html"><title>Binary numeral system - Wikipedia, the free encyclopedia</title>
    
    <style type="text/css" media="screen,projection">/*<![CDATA[*/ @import "/skins-1.5/monobook/main.css?1"; /*]]>*/</style>
    <link rel="stylesheet" type="text/css" media="print" href="Binary_numeral_system_files/commonPrint.css"><!--[if lt IE 5.5000]><style type="text/css">@import "/skins-1.5/monobook/IE50Fixes.css";</style><![endif]--><!--[if IE 5.5000]><style type="text/css">@import "/skins-1.5/monobook/IE55Fixes.css";</style><![endif]--><!--[if gte IE 6]><style type="text/css">@import "/skins-1.5/monobook/IE60Fixes.css";</style><![endif]--><!--[if IE]><script type="text/javascript" src="/skins-1.5/common/IEFixes.js"></script>
    <meta http-equiv="imagetoolbar" content="no" /><![endif]-->
    
    
    
    
    
<script language="JavaScript">
<!--

function SymError()
{
  return true;
}

window.onerror = SymError;

var SymRealWinOpen = window.open;

function SymWinOpen(url, name, attributes)
{
  return (new Object());
}

window.open = SymWinOpen;

//-->
</script>

<script type="text/javascript" src="Binary_numeral_system_files/index"></script>    <script type="text/javascript" src="Binary_numeral_system_files/wikibits.js"></script>
        <style type="text/css">/*<![CDATA[*/
@import "/w/index.php?title=MediaWiki:Common.css&action=raw&ctype=text/css&smaxage=2678400";
@import "/w/index.php?title=MediaWiki:Monobook.css&action=raw&ctype=text/css&smaxage=2678400";
@import "/w/index.php?title=-&action=raw&gen=css&maxage=2678400";
/*]]>*/</style></head>

  <body class="ns-0">
    <div id="globalWrapper">
      <div id="column-content">
	<div id="content">
	  <a name="top" id="top"></a>
	  	  <h1 class="firstHeading">Binary numeral system</h1>
	  <div id="bodyContent">
	    <h3 id="siteSub">From Wikipedia, the free encyclopedia.</h3>
	    <div id="contentSub"></div>
	    	    	    <div id="jump-to-nav">Jump to: <a href="#column-one">navigation</a>, <a href="#searchInput">search</a></div>	    <!-- start content -->
	    <table style="border: 1px solid rgb(170, 170, 170); margin: 0pt 0pt 0.5em 1em; font-size: 95%; float: right; clear: right;" border="0" cellpadding="4" cellspacing="0">
<tbody><tr>
<th style="background: rgb(204, 204, 255) none repeat scroll 0%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;"><a href="http://en.wikipedia.org/wiki/Numeral_system" title="Numeral system">Numeral systems</a></th>
</tr>
<tr>
<td>
<p><a href="http://en.wikipedia.org/wiki/Arabic_numerals" title="Arabic numerals">Arabic (Hindu)</a><br>
<a href="http://en.wikipedia.org/wiki/Abjad_numerals" title="Abjad numerals">Arabic (Abjad)</a><br>
<a href="http://en.wikipedia.org/wiki/Armenian_numerals" title="Armenian numerals">Armenian</a><br>
<a href="http://en.wikipedia.org/wiki/Attic_numerals" title="Attic numerals">Attic (Greek)</a><br>
<a href="http://en.wikipedia.org/wiki/Babylonian_numerals" title="Babylonian numerals">Babylonian</a><br>
<a href="http://en.wikipedia.org/wiki/Brahmi_numeral" title="Brahmi numeral">Brahmi</a><br>
<a href="http://en.wikipedia.org/wiki/Chinese_numerals" title="Chinese numerals">Chinese</a><br>
<a href="http://en.wikipedia.org/wiki/Cyrillic_numerals" title="Cyrillic numerals">Cyrillic</a><br>
<a href="http://en.wikipedia.org/wiki/D%27ni_numerals" title="D'ni numerals">D'ni (fictitious)</a><br>
<a href="http://en.wikipedia.org/wiki/Egyptian_numerals" title="Egyptian numerals">Egyptian</a><br>
<a href="http://en.wikipedia.org/wiki/Etruscan_numerals" title="Etruscan numerals">Etruscan</a><br>
<a href="http://en.wikipedia.org/wiki/Greek_numerals" title="Greek numerals">Greek</a><br>
<a href="http://en.wikipedia.org/wiki/Hebrew_numerals" title="Hebrew numerals">Hebrew</a><br>
<a href="http://en.wikipedia.org/wiki/Indian_numerals" title="Indian numerals">Indian</a><br>
<a href="http://en.wikipedia.org/wiki/Ionian_numerals" title="Ionian numerals">Ionian (Greek)</a><br>
<a href="http://en.wikipedia.org/wiki/Japanese_numerals" title="Japanese numerals">Japanese</a><br>
<a href="http://en.wikipedia.org/wiki/Khmer_numerals" title="Khmer numerals">Khmer</a><br>
<a href="http://en.wikipedia.org/wiki/Maya_numerals" title="Maya numerals">Mayan</a><br>
<a href="http://en.wikipedia.org/wiki/Roman_numerals" title="Roman numerals">Roman</a><br>
<a href="http://en.wikipedia.org/wiki/Thai_numerals" title="Thai numerals">Thai</a><br></p>
<hr>
<p><a href="http://en.wikipedia.org/wiki/Unary_numeral_system" title="Unary numeral system">Unary (1)</a><br>
<strong>Binary (2)</strong><br>
<a href="http://en.wikipedia.org/wiki/Ternary" title="Ternary">Ternary (3)</a><br>
<a href="http://en.wikipedia.org/wiki/Quaternary_numeral_system" title="Quaternary numeral system">Quaternary (4)</a><br>
<a href="http://en.wikipedia.org/wiki/Quinary" title="Quinary">Quinary (5)</a><br>
<a href="http://en.wikipedia.org/wiki/Senary" title="Senary">Senary (6)</a><br>
<a href="http://en.wikipedia.org/wiki/Septenary" title="Septenary">Septenary (7)</a><br>
<a href="http://en.wikipedia.org/wiki/Octal" title="Octal">Octal (8)</a><br>
<a href="http://en.wikipedia.org/wiki/Decimal" title="Decimal">Decimal (10)</a><br>
<a href="http://en.wikipedia.org/wiki/Duodecimal" title="Duodecimal">Duodecimal (12)</a><br>
<a href="http://en.wikipedia.org/wiki/Hexadecimal" title="Hexadecimal">Hexadecimal (16)</a><br>
<a href="http://en.wikipedia.org/wiki/Vigesimal" title="Vigesimal">Vigesimal (20)</a><br>
<a href="http://en.wikipedia.org/wiki/Base_24" title="Base 24">Quadrovigesimal (24)</a><br>
<a href="http://en.wikipedia.org/wiki/Hexavigesimal" title="Hexavigesimal">Hexavigesimal (26)</a><br>
<a href="http://en.wikipedia.org/wiki/Septemvigesimal" title="Septemvigesimal">Septemvigesimal (27)</a><br>
<a href="http://en.wikipedia.org/wiki/Base_36" title="Base 36">Hexatridecimal (36)</a><br>
<a href="http://en.wikipedia.org/wiki/Sexagesimal" title="Sexagesimal">Sexagesimal (60)</a><br></p>
</td>
</tr>
<tr>
<th style="text-align: right;"><span class="noprint" style="white-space: nowrap; font-size: smaller;"><a href="http://en.wikipedia.org/w/index.php?title=Template:Numeral_systems&amp;action=edit" class="external text" title="http://en.wikipedia.org/w/index.php?title=Template:Numeral systems&amp;action=edit">edit</a></span></th>
</tr>
</tbody></table>
<p>The <b>binary <a href="http://en.wikipedia.org/wiki/Numeral_system" title="Numeral system">numeral system</a></b> represents numeric values using two symbols, typically <a href="http://en.wikipedia.org/wiki/0_%28number%29" title="0 (number)">0</a> and <a href="http://en.wikipedia.org/wiki/1_%28number%29" title="1 (number)">1</a>. More specifically, binary is a <a href="http://en.wikipedia.org/wiki/Positional_notation" title="Positional notation">positional notation</a> with a <a href="http://en.wikipedia.org/wiki/Radix" title="Radix">radix</a> of <a href="http://en.wikipedia.org/wiki/2_%28number%29" title="2 (number)">two</a>. Owing to its relatively straightforward implementation in <a href="http://en.wikipedia.org/wiki/Electronic_circuit" title="Electronic circuit">electronic circuitry</a>, the binary system is used internally by virtually all modern <a href="http://en.wikipedia.org/wiki/Computer" title="Computer">computers</a>.</p>
<table id="toc" class="toc">
<tbody><tr>
<td>
<div id="toctitle">
<h2>Contents</h2>
 <span class="toctoggle">[<a href="javascript:toggleToc()" class="internal" id="togglelink">hide</a>]</span></div>
<ul>
<li class="toclevel-1"><a href="#History"><span class="tocnumber">1</span> <span class="toctext">History</span></a></li>
<li class="toclevel-1"><a href="#Representation"><span class="tocnumber">2</span> <span class="toctext">Representation</span></a></li>
<li class="toclevel-1"><a href="#Counting_in_binary"><span class="tocnumber">3</span> <span class="toctext">Counting in binary</span></a></li>
<li class="toclevel-1"><a href="#Binary_arithmetic"><span class="tocnumber">4</span> <span class="toctext">Binary arithmetic</span></a>
<ul>
<li class="toclevel-2"><a href="#Addition"><span class="tocnumber">4.1</span> <span class="toctext">Addition</span></a></li>
<li class="toclevel-2"><a href="#Subtraction"><span class="tocnumber">4.2</span> <span class="toctext">Subtraction</span></a></li>
<li class="toclevel-2"><a href="#Multiplication"><span class="tocnumber">4.3</span> <span class="toctext">Multiplication</span></a></li>
<li class="toclevel-2"><a href="#Division"><span class="tocnumber">4.4</span> <span class="toctext">Division</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="#Bitwise_logical_operations"><span class="tocnumber">5</span> <span class="toctext">Bitwise logical operations</span></a></li>
<li class="toclevel-1"><a href="#Conversion_to_and_from_other_numeral_systems"><span class="tocnumber">6</span> <span class="toctext">Conversion to and from other numeral systems</span></a>
<ul>
<li class="toclevel-2"><a href="#Decimal"><span class="tocnumber">6.1</span> <span class="toctext">Decimal</span></a></li>
<li class="toclevel-2"><a href="#Hexadecimal"><span class="tocnumber">6.2</span> <span class="toctext">Hexadecimal</span></a></li>
<li class="toclevel-2"><a href="#Octal"><span class="tocnumber">6.3</span> <span class="toctext">Octal</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="#Representing_real_numbers"><span class="tocnumber">7</span> <span class="toctext">Representing real numbers</span></a></li>
<li class="toclevel-1"><a href="#Binary_humor"><span class="tocnumber">8</span> <span class="toctext">Binary humor</span></a></li>
<li class="toclevel-1"><a href="#See_also"><span class="tocnumber">9</span> <span class="toctext">See also</span></a></li>
<li class="toclevel-1"><a href="#External_links"><span class="tocnumber">10</span> <span class="toctext">External links</span></a></li>
</ul>
</td>
</tr>
</tbody></table>
<p><script type="text/javascript">
//<![CDATA[
 if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } 
//]]>
</script></p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=1" title="Binary numeral system">edit</a>]</div>
<p><a name="History" id="History"></a></p>
<h2>History</h2>
<p>The modern binary number system was first fully documented by <a href="http://en.wikipedia.org/wiki/Gottfried_Leibniz" title="Gottfried Leibniz">Gottfried Leibniz</a> in the <a href="http://en.wikipedia.org/wiki/17th_century" title="17th century">17th century</a> in his article <i><a href="http://en.wikipedia.org/wiki/Explication_de_l%27Arithm%C3%A9tique_Binaire" title="Explication de l'Arithmétique Binaire">Explication de l'Arithmétique Binaire</a></i>. Leibniz's uses 0 and 1, like the modern binary numeral system.</p>
<p>In <a href="http://en.wikipedia.org/wiki/1854" title="1854">1854</a>, <a href="http://en.wikipedia.org/wiki/United_Kingdom" title="United Kingdom">British</a> mathematician <a href="http://en.wikipedia.org/wiki/George_Boole" title="George Boole">George Boole</a> published a landmark paper detailing a system of <a href="http://en.wikipedia.org/wiki/Logic" title="Logic">logic</a> that would become known as <a href="http://en.wikipedia.org/wiki/Boolean_algebra" title="Boolean algebra">Boolean algebra</a>.
His logical system proved instrumental in the development of the binary
system, particularly in its implementation in electronic circuitry.</p>
<p>In <a href="http://en.wikipedia.org/wiki/1937" title="1937">1937</a>, <a href="http://en.wikipedia.org/wiki/Claude_Shannon" title="Claude Shannon">Claude Shannon</a> produced his master's thesis at <a href="http://en.wikipedia.org/wiki/MIT" title="MIT">MIT</a> that implemented <a href="http://en.wikipedia.org/wiki/Boolean_algebra" title="Boolean algebra">Boolean algebra</a> and binary arithmetic using electronic relays and switches for the first time in history. Entitled <i><a href="http://en.wikipedia.org/wiki/A_Symbolic_Analysis_of_Relay_and_Switching_Circuits" title="A Symbolic Analysis of Relay and Switching Circuits">A Symbolic Analysis of Relay and Switching Circuits</a></i>, Shannon's thesis essentially founded practical <a href="http://en.wikipedia.org/wiki/Digital_circuit" title="Digital circuit">digital circuit</a> design.</p>
<p>In November of <a href="http://en.wikipedia.org/wiki/1937" title="1937">1937</a>, <a href="http://en.wikipedia.org/wiki/George_Stibitz" title="George Stibitz">George Stibitz</a>, then working at <a href="http://en.wikipedia.org/wiki/Bell_Labs" title="Bell Labs">Bell Labs</a>, completed a relay-based computer he dubbed the "Model K" (for "<b>k</b>itchen",
where he had assembled it), which calculated using binary addition.
Bell Labs thus authorized a full research program in late <a href="http://en.wikipedia.org/wiki/1938" title="1938">1938</a> with Stibitz at the helm. Their <a href="http://en.wikipedia.org/w/index.php?title=Complex_Number_Computer&amp;action=edit" class="new" title="Complex Number Computer">Complex Number Computer</a>, completed <a href="http://en.wikipedia.org/wiki/January_8" title="January 8">January 8</a>, <a href="http://en.wikipedia.org/wiki/1940" title="1940">1940</a>, was able to calculate <a href="http://en.wikipedia.org/wiki/Complex_numbers" title="Complex numbers">complex numbers</a>. In a demonstration to the <a href="http://en.wikipedia.org/wiki/American_Mathematical_Society" title="American Mathematical Society">American Mathematical Society</a> conference at <a href="http://en.wikipedia.org/wiki/Dartmouth_College" title="Dartmouth College">Dartmouth College</a> on <a href="http://en.wikipedia.org/wiki/September_11" title="September 11">September 11</a>, <a href="http://en.wikipedia.org/wiki/1940" title="1940">1940</a>, Stibitz was able to send the Complex Number Calculator remote commands over telephone lines by a <a href="http://en.wikipedia.org/wiki/Teletype" title="Teletype">teletype</a>.
It was the first computing machine ever used remotely over a phone
line. Some participants of the conference who witnessed the
demonstration were <a href="http://en.wikipedia.org/wiki/John_Von_Neumann" title="John Von Neumann">John Von Neumann</a>, <a href="http://en.wikipedia.org/wiki/John_Mauchly" title="John Mauchly">John Mauchly</a>, and <a href="http://en.wikipedia.org/wiki/Norbert_Wiener" title="Norbert Wiener">Norbert Wiener</a>, who wrote about it in his memoirs.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=2" title="Binary numeral system">edit</a>]</div>
<p><a name="Representation" id="Representation"></a></p>
<h2>Representation</h2>
<p>A binary number can be represented by any sequence of <a href="http://en.wikipedia.org/wiki/Bit" title="Bit">bits</a>
(binary digits), which in turn may be represented by any mechanism
capable of being in two mutually exclusive states. The following
sequences of symbols could all be interpreted as different binary
numeric values:</p>
<pre>1 0 1 0 0 1 1
- | | - -
x x x o x o o
n y y n
</pre>
<div class="thumb tright">
<div style="width: 302px;"><a href="http://en.wikipedia.org/wiki/Image:Binary_clock.png" class="internal" title="A binary clock might use LEDs to express binary values. In this clock, each column of LEDs shows a binary-coded decimal numeral of the traditional sexagesimal time. "><img src="Binary_numeral_system_files/300px-Binary_clock.png" alt="A binary clock might use LEDs to express binary values. In this clock, each column of LEDs shows a binary-coded decimal numeral of the traditional sexagesimal time. " longdesc="/wiki/Image:Binary_clock.png" height="258" width="300"></a>
<div class="thumbcaption">
<div class="magnify" style="float: right;"><a href="http://en.wikipedia.org/wiki/Image:Binary_clock.png" class="internal" title="Enlarge"><img src="Binary_numeral_system_files/magnify-clip.png" alt="Enlarge" height="11" width="15"></a></div>
A <a href="http://en.wikipedia.org/wiki/Binary_clock" title="Binary clock">binary clock</a> might use <a href="http://en.wikipedia.org/wiki/Light-emitting_diode" title="Light-emitting diode">LEDs</a> to express binary values. In this clock, each column of LEDs shows a <a href="http://en.wikipedia.org/wiki/Binary-coded_decimal" title="Binary-coded decimal">binary-coded decimal</a> numeral of the traditional <a href="http://en.wikipedia.org/wiki/Sexagesimal" title="Sexagesimal">sexagesimal</a> time.</div>
</div>
</div>
<p>The numeric value represented in each case is dependent upon the
value assigned to each symbol. In a computer, the numeric values may be
represented by two different <a href="http://en.wikipedia.org/wiki/Voltage" title="Voltage">voltages</a>; on a <a href="http://en.wikipedia.org/wiki/Magnetic_field" title="Magnetic field">magnetic</a> <a href="http://en.wikipedia.org/wiki/Disk" title="Disk">disk</a>, magnetic <a href="http://en.wikipedia.org/wiki/Polarity" title="Polarity">polarities</a>
may be used. A "positive", "yes", or "on" state is not necessarily
equivalent to the numerical value of one; it depends on the
architecture in use.</p>
<p>In keeping with customary representation of numerals using <a href="http://en.wikipedia.org/wiki/Arabic_numerals" title="Arabic numerals">arabic numerals</a>, binary numbers are commonly written using the symbols <b>0</b> and <b>1</b>.
When written, binary numerals are often subscripted or suffixed in
order to indicate their base, or radix. The following notations are
equivalent:</p>
<dl>
<dd>100101 binary (explicit statement of format)</dd>
<dd>100101b (a suffix indicating binary format)</dd>
<dd>bin 100101 (a prefix indicating binary format)</dd>
<dd>100101<sub>2</sub> (a subscript indicating base-2 notation)</dd>
</dl>
<p>When spoken, binary numerals are usually pronounced by pronouncing
each individual digit, in order to distinguish them from decimal
numbers. For example, the binary numeral "100" is pronounced "one zero
zero", rather than "one hundred", to make its binary nature explicit,
and for purposes of correctness. Since the binary numeral "100" is
equal to the decimal value four, it would be confusing, and numerically
incorrect, to refer to the numeral as "one hundred."</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=3" title="Binary numeral system">edit</a>]</div>
<p><a name="Counting_in_binary" id="Counting_in_binary"></a></p>
<h2>Counting in binary</h2>
<p>Counting in binary is similar to counting in any other number
system. Beginning with a single digit, counting proceeds through each
symbol, in increasing order. Decimal counting uses the symbols <b>0</b> through <b>9</b>, while binary only uses the symbols <b>0</b> and <b>1</b>.</p>
<p>When the symbols for the first digit are exhausted, the next-higher
digit (to the left) is incremented, and counting starts over at 0. In <a href="http://en.wikipedia.org/wiki/Decimal" title="Decimal">decimal</a>, counting proceeds like so:</p>
<dl>
<dd>00, 01, 02, ... 07, 08, 09 (rightmost digit starts over, and the 0 is incremented)</dd>
<dd><b>1</b>0, 11, 12, ... 17, 18, 19 (rightmost digit starts over, and the 1 is incremented)</dd>
<dd><b>2</b>0, 21, 22, ...</dd>
</dl>
<p>When the rightmost digit reaches 9, counting returns to 0, and the
second digit is incremented. In binary, counting is similar, with the
exception that only the two symbols <b>0</b> and <b>1</b> are used. When 1 is reached, counting begins at 0 again, with the digit to the left being incremented:</p>
<dl>
<dd>000, 001 (rightmost digit starts over, and the second 0 is incremented)</dd>
<dd>0<b>1</b>0, 011 (middle and rightmost digits start over, and the first 0 is incremented)</dd>
<dd><b>1</b>00, 101 (rightmost digit starts over again, middle 0 is incremented)</dd>
<dd>1<b>1</b>0, 111...</dd>
</dl>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=4" title="Binary numeral system">edit</a>]</div>
<p><a name="Binary_arithmetic" id="Binary_arithmetic"></a></p>
<h2>Binary arithmetic</h2>
<p>Arithmetic in binary is much like arithmetic in other numeral
systems. Addition, subtraction, multiplication, and division can be
performed on binary numerals.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=5" title="Binary numeral system">edit</a>]</div>
<p><a name="Addition" id="Addition"></a></p>
<h3>Addition</h3>
<div class="thumb tright">
<div style="width: 202px;"><a href="http://en.wikipedia.org/wiki/Image:ALU_half_adder.png" class="internal" title="The circuit diagram for a binary half adder, which adds two bits together, producing sum and carry bits."><img src="Binary_numeral_system_files/200px-ALU_half_adder.png" alt="The circuit diagram for a binary half adder, which adds two bits together, producing sum and carry bits." longdesc="/wiki/Image:ALU_half_adder.png" height="119" width="200"></a>
<div class="thumbcaption">
<div class="magnify" style="float: right;"><a href="http://en.wikipedia.org/wiki/Image:ALU_half_adder.png" class="internal" title="Enlarge"><img src="Binary_numeral_system_files/magnify-clip.png" alt="Enlarge" height="11" width="15"></a></div>
The <a href="http://en.wikipedia.org/wiki/Circuit_diagram" title="Circuit diagram">circuit diagram</a> for a binary <a href="http://en.wikipedia.org/wiki/Adder_%28electronics%29" title="Adder (electronics)">half adder</a>, which adds two bits together, producing sum and carry bits.</div>
</div>
</div>
<p>The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple:</p>
<dl>
<dd>0 + 0 = 0</dd>
<dd>0 + 1 = 1</dd>
<dd>1 + 0 = 1</dd>
<dd>1 + 1 = 10 (the 1 is carried)</dd>
</dl>
<p>Adding two "1" values produces the value "10", equivalent to the
decimal value 2. This is similar to what happens in decimal when
certain single-digit numbers are added together; if the result exceeds
the value of the radix (10), the digit to the left is incremented:</p>
<dl>
<dd>5 + 5 = 10</dd>
<dd>7 + 9 = 16</dd>
</dl>
<p>This is known as <i>carrying</i> in most numeral systems. When the
result of an addition exceeds the value of the radix, the procedure is
to "carry the one" to the left, adding it to the next positional value.
Carrying works the same way in binary:</p>
<pre>  1 1 1 1 1     (carry)
    0 1 1 0 1
+   1 0 1 1 1
-------------
= 1 0 0 1 0 0
</pre>
<p>In this example, two numerals are being added together: 01101 (13
decimal) and 10111 (23 decimal). The top row shows the carry bits used.
Starting in the rightmost column, 1 + 1 = 10. The 1 is carried to the
left, and the 0 is written at the bottom of the rightmost column. The
second column from the right is added: 1 + 0 + 1 = 10 again; the 1 is
carried, and 0 is written at the bottom. The third column: 1 + 1 + 1 =
11. This time, a 1 is carried, and a 1 is written in the bottom row.
Proceeding like this gives the final answer 100100.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=6" title="Binary numeral system">edit</a>]</div>
<p><a name="Subtraction" id="Subtraction"></a></p>
<h3>Subtraction</h3>
<p>Subtraction works in much the same way:</p>
<dl>
<dd>0 - 0 = 0</dd>
<dd>0 - 1 = 1 (with borrow)</dd>
<dd>1 - 0 = 1</dd>
<dd>1 - 1 = 0</dd>
</dl>
<p>One binary numeral can be subtracted from another as follows:</p>
<pre>    *   * * *   (starred columns are borrowed from)
  1 1 0 1 1 1 0
-     1 0 1 1 1
----------------
= 1 0 1 0 1 1 1
</pre>
<p>Subtracting a positive number is equivalent to <i>adding</i> a <a href="http://en.wikipedia.org/wiki/Negative_and_non-negative_numbers" title="Negative and non-negative numbers">negative</a> number of equal <a href="http://en.wikipedia.org/wiki/Absolute_value" title="Absolute value">absolute value</a>; computers typically use the <a href="http://en.wikipedia.org/wiki/Two%27s_complement" title="Two's complement">two's complement</a>
notation to represent negative values. This notation eliminates the
need for a separate "subtract" operation. For further details, see <a href="http://en.wikipedia.org/wiki/Two%27s_complement" title="Two's complement">two's complement</a>.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=7" title="Binary numeral system">edit</a>]</div>
<p><a name="Multiplication" id="Multiplication"></a></p>
<h3>Multiplication</h3>
<p>Multiplication in binary is similar to its decimal counterpart. Two numbers <i>A</i> and <i>B</i> can be multiplied by partial products: for each digit in <i>B</i>, the product of that digit in <i>A</i> is calculated and written on a new line, shifted leftward so that its rightmost digit lines up with the digit in <i>B</i> that was used. The sum of all these partial products gives the final result.</p>
<p>Since there are only two digits in binary, there are only two possible outcomes of each partial multiplication:</p>
<ul>
<li>If the digit in <i>B</i> is 0, the partial product is also 0</li>
<li>If the digit in <i>B</i> is 1, the partial product is equal to <i>A</i></li>
</ul>
<p>For example, the binary numbers 1011 and 1010 are multiplied as follows:</p>
<pre>           1 0 1 1   (A)
         × 1 0 1 0   (B)
         ---------
           0 0 0 0   ← Corresponds to a zero in B
         1 0 1 1     ← Corresponds to a one in B
       0 0 0 0
   + 1 0 1 1
   ---------------
   = 1 1 0 1 1 1 0
</pre>
<p>See also <a href="http://en.wikipedia.org/wiki/Booth%27s_multiplication_algorithm" title="Booth's multiplication algorithm">Booth's multiplication algorithm</a>.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=8" title="Binary numeral system">edit</a>]</div>
<p><a name="Division" id="Division"></a></p>
<h3>Division</h3>
<p>Binary division is again similar to its decimal counterpart:</p>
<pre>        __________
1 0 1  | 1 1 0 1 1
</pre>
<p>Here, the divisor is 101, or 5 decimal, while the dividend is 11011,
or 27 decimal. The procedure is the same as that of decimal <a href="http://en.wikipedia.org/wiki/Long_division" title="Long division">long division</a>;
here, the divisor 101 goes into the first three digits 110 of the
dividend one time, so a "1" is written on the top line. This result is
multiplied by the divisor, and subtracted from the first three digits
of the dividend; the next digit (a "1") is included to obtain a new
three-digit sequence:</p>
<pre>             1
        __________
1 0 1  | 1 1 0 1 1
       - 1 0 1
         -----
           0 1 1
</pre>
<p>The procedure is then repeated with the new sequence, continuing until the digits in the dividend have been exhausted:</p>
<pre>             1 0 1
        __________
1 0 1  | 1 1 0 1 1
       - 1 0 1
         -----
           0 1 1
         - 0 0 0
           -----
             1 1 1
           - 1 0 1
             -----
               1 0
</pre>
<p>Thus, the quotient of 11011 divided by 101 is 101<sub>2</sub>, as shown on the top line, while the remainder, shown on the bottom line, is 10<sub>2</sub>. In decimal, 27 divided by 5 is 5, with a remainder of 2.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=9" title="Binary numeral system">edit</a>]</div>
<p><a name="Bitwise_logical_operations" id="Bitwise_logical_operations"></a></p>
<h2>Bitwise logical operations</h2>
<p>Though not directly related to the numerical interpretation of binary symbols, sequences of bits may be manipulated using <a href="http://en.wikipedia.org/wiki/Boolean_algebra" title="Boolean algebra">Boolean</a> <a href="http://en.wikipedia.org/wiki/Logical_operator" title="Logical operator">logical operators</a>. When a string of binary symbols is manipulated in this way, it is called a <a href="http://en.wikipedia.org/wiki/Bitwise_operation" title="Bitwise operation">bitwise operation</a>; the logical operators <a href="http://en.wikipedia.org/wiki/Logical_conjunction" title="Logical conjunction">AND</a>, <a href="http://en.wikipedia.org/wiki/Logical_disjunction" title="Logical disjunction">OR</a>, and <a href="http://en.wikipedia.org/wiki/Exclusive_disjunction" title="Exclusive disjunction">XOR</a> may be performed on corresponding bits in two binary numerals provided as input. The logical <a href="http://en.wikipedia.org/wiki/Negation" title="Negation">NOT</a>
operation may be performed on individual bits in a single binary
numeral provided as input. Sometimes, such operations may be used as
arithmetic short-cuts, and may have other computational benefits as
well. For example, discarding the last bit of a binary number (also
known as binary shifting), is the decimal equivalent of division by
two. See <a href="http://en.wikipedia.org/wiki/Bitwise_operation" title="Bitwise operation">Bitwise operation</a>.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=10" title="Binary numeral system">edit</a>]</div>
<p><a name="Conversion_to_and_from_other_numeral_systems" id="Conversion_to_and_from_other_numeral_systems"></a></p>
<h2>Conversion to and from other numeral systems</h2>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=11" title="Binary numeral system">edit</a>]</div>
<p><a name="Decimal" id="Decimal"></a></p>
<h3>Decimal</h3>
<p>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.</p>
<p>In place-value numeral systems, digits in successively lower, or
less significant, positions represent successively smaller powers of
the <a href="http://en.wikipedia.org/wiki/Radix" title="Radix">radix</a>.
The starting exponent is one less than the number of digits in the
number. A five-digit number would start with an exponent of four. In
the decimal system, the radix is 10 (ten), so the left-most digit of a
five-digit number represents the 10<sup>4</sup> (ten thousands) position. Consider:</p>
<dl>
<dd><b>97352<sub>10</sub></b> is equal to:
<dl>
<dd><b>9</b> times 10<sup>4</sup> (9 × 10000 = <b>90000</b>) plus</dd>
<dd><b>7</b> times 10<sup>3</sup> (7 × 1000 = <b>7000</b>) plus</dd>
<dd><b>3</b> times 10<sup>2</sup> (3 × 100 = <b>300</b>) plus</dd>
<dd><b>5</b> times 10<sup>1</sup> (5 × 10 = <b>50</b>) plus</dd>
<dd><b>2</b> times 10<sup>0</sup> (2 × 1 = <b>2</b>)</dd>
</dl>
</dd>
</dl>
<p>Multiplication by the radix is simple. The digits are shifted left,
and a 0 is appended to the right end of the number. For example, <b>9735</b> times 10 is equal to <b>97350</b>. So one way to interpret a string of digits is as the last digit added to the radix times all but the last digit. <b>97352</b> equals <b>9735</b> times 10 plus <b>2</b>. An example in binary is <b>1101100111<sub>2</sub></b> equals <b>110110011<sub>2</sub></b> times 2 plus <b>1</b>.
This is the essence of the conversion method. At each step, write the
number to be converted as 2*k + 0 or 2*k + 1 for an integer k, which
becomes the new number to be converted.</p>
<dl>
<dd><b>118<sub>10</sub></b> equals
<dl>
<dd><b>59</b> x 2 + <b>0</b></dd>
<dd>(<b>29</b> x 2 + <b>1</b>) x 2 + <b>0</b></dd>
<dd>((<b>14</b> x 2 + <b>1</b>) x 2 + <b>1</b>) x 2 + <b>0</b></dd>
<dd>(((<b>7</b> x 2 + <b>0</b>) x 2 + <b>1</b>) x 2 + <b>1</b>) x 2 + <b>0</b></dd>
<dd>((((<b>3</b> x 2 + <b>1</b>) x 2 + <b>0</b>) x 2 + <b>1</b>) x 2 + <b>1</b>) x 2 + <b>0</b></dd>
<dd>(((((<b>1</b> x 2 + <b>1</b>) x 2 + <b>1</b>) x 2 + <b>0</b>) x 2 + <b>1</b>) x 2 + <b>1</b>) x 2 + <b>0</b></dd>
<dd><b>1</b> x 2<sup>6</sup> + <b>1</b> x 2<sup>5</sup> + <b>1</b> x 2<sup>4</sup> + <b>0</b> x 2<sup>3</sup> + <b>1</b> x 2<sup>2</sup> + <b>1</b> x 2<sup>1</sup> + <b>0</b> x 2<sup>0</sup></dd>
<dd><b>1110110<sub>2</sub></b></dd>
</dl>
</dd>
</dl>
<p>So in the algorithm to convert from an integer decimal numeral to
its binary equivalent, the number is divided by two, and the remainder
written in the ones-place. The result is again divided by two, its
remainder written in the next place to the left. This process repeats
until the number becomes zero.</p>
<p>For example, 118<sub>10</sub>, in binary, is:</p>
<table border="1">
<tbody><tr>
<th>Operation</th>
<th>Remainder</th>
</tr>
<tr>
<td>118/2 = 59</td>
<td align="center">0</td>
</tr>
<tr>
<td>59/2 = 29</td>
<td align="center">1</td>
</tr>
<tr>
<td>29/2 = 14</td>
<td align="center">1</td>
</tr>
<tr>
<td>14/2 = 7</td>
<td align="center">0</td>
</tr>
<tr>
<td>7/2 = 3</td>
<td align="center">1</td>
</tr>
<tr>
<td>3/2 = 1</td>
<td align="center">1</td>
</tr>
<tr>
<td>1/2 = 0</td>
<td align="center">1</td>
</tr>
</tbody></table>
<p>Reading the sequence of remainders from the bottom up gives the binary numeral 1110110<sub>2</sub>.</p>
<p>To convert from binary to decimal 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 110010101101<sub>2</sub> to decimal:</p>
<table border="1">
<tbody><tr>
<th>Result</th>
<th>Remaining digits</th>
</tr>
<tr>
<td><b>0</b></td>
<td align="right">110010101101</td>
</tr>
<tr>
<td>0*2 + 1 = <b>1</b></td>
<td align="right">10010101101</td>
</tr>
<tr>
<td>1*2 + 1 = <b>3</b></td>
<td align="right">0010101101</td>
</tr>
<tr>
<td>3*2 + 0 = <b>6</b></td>
<td align="right">010101101</td>
</tr>
<tr>
<td>6*2 + 0 = <b>12</b></td>
<td align="right">10101101</td>
</tr>
<tr>
<td>12*2 + 1 = <b>25</b></td>
<td align="right">0101101</td>
</tr>
<tr>
<td>25*2 + 0 = <b>50</b></td>
<td align="right">101101</td>
</tr>
<tr>
<td>50*2 + 1 = <b>101</b></td>
<td align="right">01101</td>
</tr>
<tr>
<td>101*2 + 0 = <b>202</b></td>
<td align="right">1101</td>
</tr>
<tr>
<td>202*2 + 1 = <b>405</b></td>
<td align="right">101</td>
</tr>
<tr>
<td>405*2 + 1 = <b>811</b></td>
<td align="right">01</td>
</tr>
<tr>
<td>811*2 + 0 = <b>1622</b></td>
<td align="right">1</td>
</tr>
<tr>
<td>1622*2 + 1 = <b>3245</b></td>
<td align="right"></td>
</tr>
</tbody></table>
<p>and the result is 3245<sub>10</sub>.</p>
<p>The fractional parts of a numbers are converted with similar
methods. They are again based on the equivalence of shifting with
doubling or halving.</p>
<p>In a fractional binary number such as .11010110101<sub>2</sub>, the first digit is 1/2, the second 1/2<sup>2</sup>,
etc. So if there is a 1 in the first place after the decimal, then the
number is at least 1/2, 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.</p>
<p>For example, (1/3)<sub>10</sub>, in binary, is:</p>
<table border="1">
<tbody><tr>
<th>Converting</th>
<th>Result</th>
</tr>
<tr>
<td><b>1/3</b></td>
<td>0.</td>
</tr>
<tr>
<td>1/3 * 2 = <b>2/3</b> &lt; 1</td>
<td>0.0</td>
</tr>
<tr>
<td>2/3 * 2 = <b>1 1/3</b> ≥ 1</td>
<td>0.01</td>
</tr>
<tr>
<td>1/3 * 2 = <b>2/3</b> &lt; 1</td>
<td>0.010</td>
</tr>
<tr>
<td>2/3 * 2 = <b>1 1/3</b> ≥ 1</td>
<td>0.0101</td>
</tr>
</tbody></table>
<p>which is the repeating fraction 0.01<u>01</u>...<sub>2</sub></p>
<p>Or for example, 0.1<sub>10</sub>, in binary, is:</p>
<table border="1">
<tbody><tr>
<th>Converting</th>
<th>Result</th>
</tr>
<tr>
<td><b>0.1</b></td>
<td>0.</td>
</tr>
<tr>
<td>0.1 * 2 = <b>0.2</b> &lt; 1</td>
<td>0.0</td>
</tr>
<tr>
<td>0.2 * 2 = <b>0.4</b> &lt; 1</td>
<td>0.00</td>
</tr>
<tr>
<td>0.4* 2 = <b>0.8</b> &lt; 1</td>
<td>0.000</td>
</tr>
<tr>
<td>0.8* 2 = <b>1.6</b> ≥ 1</td>
<td>0.0001</td>
</tr>
<tr>
<td>0.6 * 2 = <b>1.2</b> ≥ 1</td>
<td>0.00011</td>
</tr>
<tr>
<td>0.2 * 2 = <b>0.4</b> &lt; 1</td>
<td>0.000110</td>
</tr>
<tr>
<td>0.4 * 2 = <b>0.8</b> &lt; 1</td>
<td>0.0001100</td>
</tr>
<tr>
<td>0.8 * 2 = <b>1.6</b> ≥ 1</td>
<td>0.00011001</td>
</tr>
<tr>
<td>0.6 * 2 = <b>1.2</b> ≥ 1</td>
<td>0.000110011</td>
</tr>
<tr>
<td>0.2 * 2 = <b>0.4</b> &lt; 1</td>
<td>0.0001100110</td>
</tr>
</tbody></table>
<p>which is also a repeating fraction 0.00011<u>0011</u>...<sub>2</sub>
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.</p>
<p>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,</p>
<table>
<tbody><tr>
<td>x</td>
<td align="right">=</td>
<td align="right"><b>1100</b></td>
<td align="left"><b>.1011100<u>11100</u>...</b></td>
</tr>
<tr>
<td>x times 2<sup>6</sup></td>
<td align="right">=</td>
<td align="right"><b>1100101110</b></td>
<td align="left"><b>.01110<u>01110</u>...</b></td>
</tr>
<tr>
<td>x times 2</td>
<td align="right">=</td>
<td align="right"><b>11001</b></td>
<td align="left"><b>.01110<u>01110</u>...</b></td>
</tr>
<tr>
<td>x times (2<sup>6</sup> - 2)</td>
<td align="right">=</td>
<td align="right"><b>1100010101</b></td>
</tr>
<tr>
<td>x</td>
<td align="right">=</td>
<td align="right">(789/62)<sub>10</sub></td>
</tr>
</tbody></table>
<p>Another way, perhaps quicker and more efficient than the previous,
of converting from binary to decimal, is to do so indirectly- first
converting (x binary) or (x decimal) to (x <a href="http://en.wikipedia.org/wiki/Hexidecimal" title="Hexidecimal">hexidecimal</a>) and then converting (x hexidecimal) to the opposite of the former, respectively.</p>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=12" title="Binary numeral system">edit</a>]</div>
<p><a name="Hexadecimal" id="Hexadecimal"></a></p>
<h3>Hexadecimal</h3>
<p>Binary may be converted to and from <a href="http://en.wikipedia.org/wiki/Hexadecimal" title="Hexadecimal">hexadecimal</a> somewhat more easily. This is due to the fact that the <a href="http://en.wikipedia.org/wiki/Radix" title="Radix">radix</a> 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 exactly four digits of binary to represent one digit of hexadecimal.</p>
<p>The following table shows each hexadecimal digit along with the equivalent four-digit binary sequence:</p>
<table align="center" border="0" cellpadding="8" cellspacing="0">
<tbody><tr>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Hex</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">0</td>
<td align="center">0000</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">0001</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">0010</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">0011</td>
</tr>
</tbody></table>
</td>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Hex</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">4</td>
<td align="center">0100</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">0101</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">0110</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">0111</td>
</tr>
</tbody></table>
</td>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Hex</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">8</td>
<td align="center">1000</td>
</tr>
<tr>
<td align="center">9</td>
<td align="center">1001</td>
</tr>
<tr>
<td align="center">A</td>
<td align="center">1010</td>
</tr>
<tr>
<td align="center">B</td>
<td align="center">1011</td>
</tr>
</tbody></table>
</td>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Hex</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">C</td>
<td align="center">1100</td>
</tr>
<tr>
<td align="center">D</td>
<td align="center">1101</td>
</tr>
<tr>
<td align="center">E</td>
<td align="center">1110</td>
</tr>
<tr>
<td align="center">F</td>
<td align="center">1111</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<p>To convert a hexadecimal number into its binary equivalent, simply substitute the corresponding binary digits:</p>
<dl>
<dd>3A<sub>16</sub> = 0011 1010<sub>2</sub></dd>
<dd>E7<sub>16</sub> = 1110 0111<sub>2</sub></dd>
</dl>
<p>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 <b>0</b> bits at the left (called <a href="http://en.wikipedia.org/wiki/Padding" title="Padding">padding</a>). For example:</p>
<dl>
<dd>1010010<sub>2</sub> = 0101 0010 grouped with padding = 52<sub>16</sub></dd>
<dd>11011101<sub>2</sub> = 1101 1101 grouped = DD<sub>16</sub></dd>
</dl>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=13" title="Binary numeral system">edit</a>]</div>
<p><a name="Octal" id="Octal"></a></p>
<h3>Octal</h3>
<p>Binary is also easily converted to the <a href="http://en.wikipedia.org/wiki/Octal" title="Octal">octal</a> numeral system, since octal uses a radix of 8, which is a <a href="http://en.wikipedia.org/wiki/Power_of_two" title="Power of two">power of two</a> (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 on.</p>
<table align="center" border="0" cellpadding="8" cellspacing="0">
<tbody><tr>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Octal</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">0</td>
<td align="center">000</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">001</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">010</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">011</td>
</tr>
</tbody></table>
</td>
<td>
<table bgcolor="#f0f0f0" border="2" cellpadding="2">
<tbody><tr>
<th>Octal</th>
<th>Binary</th>
</tr>
<tr>
<td align="center">4</td>
<td align="center">100</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">101</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">110</td>
</tr>
<tr>
<td align="center">7</td>
<td align="center">111</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<p>Converting from octal to decimal proceeds in the same fashion as it does for hexadecimal:</p>
<dl>
<dd>65<sub>8</sub> = 110 101<sub>2</sub></dd>
<dd>17<sub>8</sub> = 001 111<sub>2</sub></dd>
</dl>
<p>And from binary to octal:</p>
<dl>
<dd>101100<sub>2</sub> = 101 100<sub>2</sub> grouped = 54<sub>8</sub></dd>
<dd>10011<sub>2</sub> = 010 011<sub>2</sub> grouped with padding = 23<sub>8</sub></dd>
</dl>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=14" title="Binary numeral system">edit</a>]</div>
<p><a name="Representing_real_numbers" id="Representing_real_numbers"></a></p>
<h2>Representing real numbers</h2>
<p>Non-integers can be represented by using negative powers, which are set off from the other digits by means of a <a href="http://en.wikipedia.org/wiki/Radix_point" title="Radix point">radix point</a> (called a <a href="http://en.wikipedia.org/wiki/Decimal_point" title="Decimal point">decimal point</a> in the decimal system). For example, the binary number 11.01<sub>2</sub> thus means:</p>
<dl>
<dd><b>1</b> times 2<sup>1</sup> (1 × 2 = <b>2</b>) plus</dd>
<dd><b>1</b> times 2<sup>0</sup> (1 × 1 = <b>1</b>) plus</dd>
<dd><b>0</b> times 2<sup>-1</sup> (0 × (1/2) = <b>0</b>) plus</dd>
<dd><b>1</b> times 2<sup>-2</sup> (1 × (1/4) = <b>0.25</b>)</dd>
</dl>
<p>For a total of 3.25 decimal.</p>
<p>All <a href="http://en.wikipedia.org/wiki/Dyadic_fraction" title="Dyadic fraction">dyadic rational numbers</a> p/2<sup>a</sup> have a <i>terminating</i> binary numeral -- the binary representation has only finitely many terms after the radix point. Other <a href="http://en.wikipedia.org/wiki/Rational_number" title="Rational number">rational numbers</a> have binary representation, but instead of terminating, they <i>recur</i>, with a finite sequence of digits repeating indefinitely. For instance</p>
<dl>
<dd>1/3<sub>10</sub> = 1/11<sub>2</sub> = 0.0101010101...<sub>2</sub></dd>
<dd>12<sub>10</sub>/17<sub>10</sub> = 1100<sub>2</sub> / 10001<sub>2</sub> = 0.10110100 10110100 10110100...<sub>2</sub></dd>
</dl>
<p>The phenomenon that the binary representation of any rational is
either terminating or recurring also occurs in other radix-based
numeral systems. See, for instance, the explanation in <a href="http://en.wikipedia.org/wiki/Decimal" title="Decimal">Decimal</a>.
Another similarity is the existence of alternative representations for
any terminating representation, relying on the fact that 0.111111... is
the sum of the <a href="http://en.wikipedia.org/wiki/Geometric_series" title="Geometric series">geometric series</a> 2<sup>-1</sup> + 2<sup>-2</sup> + 2<sup>-3</sup> + ... which is 1.</p>
<p>Binary numerals which neither terminate nor recur represent <a href="http://en.wikipedia.org/wiki/Irrational_number" title="Irrational number">irrational numbers</a>. For instance,</p>
<ul>
<li>0.10100100010000100000100.... does have a pattern, but it is not a fixed-length recurring pattern, so the number is irrational</li>
<li>1.0110101000001001111001100110011111110... is the binary
representation of √2, the square root of 2, another irrational. It has
no discernible pattern, although a proof that √2 is irrational requires
more than this. See <a href="http://en.wikipedia.org/wiki/Irrational_number" title="Irrational number">irrational number</a>.</li>
</ul>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=15" title="Binary numeral system">edit</a>]</div>
<p><a name="Binary_humor" id="Binary_humor"></a></p>
<h2>Binary humor</h2>
<ul>
<li>"Binary is as easy as 1, 10, 11."</li>
<li>"There are 10 kinds of people in the world - those who understand binary numbers, and those who don't."</li>
</ul>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=16" title="Binary numeral system">edit</a>]</div>
<p><a name="See_also" id="See_also"></a></p>
<h2>See also</h2>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Binary-coded_decimal" title="Binary-coded decimal">Binary-coded decimal</a></li>
<li><a href="http://en.wikipedia.org/wiki/Pingala" title="Pingala">Pingala</a></li>
</ul>
<div class="editsection" style="float: right; margin-left: 5px;">[<a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit&amp;section=17" title="Binary numeral system">edit</a>]</div>
<p><a name="External_links" id="External_links"></a></p>
<h2>External links</h2>
<ul>
<li><a href="http://www.insidereality.net/site/content/math/base_conversion.php" class="external text" title="http://www.insidereality.net/site/content/math/base conversion.php">Simple Conversion Methods</a></li>
<li><a href="http://www-history.mcs.st-andrews.ac.uk/history/Projects/Pearce/index.html" class="external text" title="http://www-history.mcs.st-andrews.ac.uk/history/Projects/Pearce/index.html">Indian mathematics</a></li>
<li><a href="http://www.cut-the-knot.org/binary.shtml" class="external text" title="http://www.cut-the-knot.org/binary.shtml">Base Converter</a></li>
<li><a href="http://www.cut-the-knot.org/do_you_know/BinaryHistory.shtml" class="external text" title="http://www.cut-the-knot.org/do you know/BinaryHistory.shtml">Binary System</a></li>
<li><a href="http://www.cut-the-knot.org/blue/frac_conv.shtml" class="external text" title="http://www.cut-the-knot.org/blue/frac conv.shtml">Conversion of Fractions</a></li>
<li><a href="http://leetkey.mozdev.org/" class="external text" title="http://leetkey.mozdev.org">This FireFox extension supports ASCII/Binary conversions and typing</a></li>
<li><a href="http://www.permadi.com/tutorial/numHexToDec/" class="external text" title="http://www.permadi.com/tutorial/numHexToDec/">Converting Hexadecimal to Decimal</a></li>
<li><a href="http://www.permadi.com/tutorial/numDecToHex/" class="external text" title="http://www.permadi.com/tutorial/numDecToHex/">Converting Decimal to Hexadecimal</a></li>
</ul>


<!-- Saved in parser cache with key enwiki:pcache:idhash:238686-0!1!0!0!!en!2 and timestamp 20051121151258 -->
<div class="printfooter">
Retrieved from "<a href="http://en.wikipedia.org/wiki/Binary_numeral_system">http://en.wikipedia.org/wiki/Binary_numeral_system</a>"</div>
	    <div id="catlinks"><p class="catlinks"><a href="http://en.wikipedia.org/w/index.php?title=Special:Categories&amp;article=Binary_numeral_system" title="Special:Categories">Categories</a>: <a href="http://en.wikipedia.org/wiki/Category:Numeration" title="Category:Numeration">Numeration</a> | <a href="http://en.wikipedia.org/wiki/Category:Computer_arithmetic" title="Category:Computer arithmetic">Computer arithmetic</a> | <a href="http://en.wikipedia.org/wiki/Category:Elementary_arithmetic" title="Category:Elementary arithmetic">Elementary arithmetic</a> | <a href="http://en.wikipedia.org/wiki/Category:Positional_numeral_systems" title="Category:Positional numeral systems">Positional numeral systems</a></p></div>	    <!-- end content -->
	    <div class="visualClear"></div>
	  </div>
	</div>
      </div>
      <div id="column-one">
	<div id="p-cactions" class="portlet">
	  <h5>Views</h5>
	  <ul>
	    <li id="ca-nstab-main" class="selected"><a title="View the content page [alt-c]" accesskey="c" href="http://en.wikipedia.org/wiki/Binary_numeral_system">Article</a></li><li id="ca-talk"><a title="Discussion about the content page [alt-t]" accesskey="t" href="http://en.wikipedia.org/wiki/Talk:Binary_numeral_system">Discussion</a></li><li id="ca-edit"><a title="You can edit this page. Please use the preview button before saving. [alt-e]" accesskey="e" href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=edit">Edit this page</a></li><li id="ca-history"><a title="Past versions of this page. [alt-h]" accesskey="h" href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;action=history">History</a></li>	  </ul>
	</div>
	<div class="portlet" id="p-personal">
	  <h5>Personal tools</h5>
	  <div class="pBody">
	    <ul>
	    <li id="pt-login"><a title="Logging in is not required, but brings many benefits. [alt-o]" accesskey="o" href="http://en.wikipedia.org/w/index.php?title=Special:Userlogin&amp;returnto=Binary_numeral_system">Sign in / create account</a></li>	    </ul>
	  </div>
	</div>
	<div title="Main Page" class="portlet" id="p-logo">
	  <a style="background-image: url(/images/wiki-en.png);" href="http://en.wikipedia.org/wiki/Main_Page" title="Main Page"></a>
	</div>
	<script type="text/javascript"> if (window.isMSIE55) fixalpha(); </script>
		<div class="portlet" id="p-navigation">
	  <h5>Navigation</h5>
	  <div class="pBody">
	    <ul>
	    <li id="n-mainpage"><a title="Visit the Main Page [alt-z]" accesskey="z" href="http://en.wikipedia.org/wiki/Main_Page">Main Page</a></li><li title="About the project, what you can do, where to find things" id="n-portal"><a href="http://en.wikipedia.org/wiki/Wikipedia:Community_Portal">Community Portal</a></li><li title="Find background information on current events" id="n-currentevents"><a href="http://en.wikipedia.org/wiki/Current_events">Current events</a></li><li id="n-recentchanges"><a title="List of recent changes in Wikipedia [alt-r]" accesskey="r" href="http://en.wikipedia.org/wiki/Special:Recentchanges">Recent changes</a></li><li id="n-randompage"><a title="Load a random page [alt-x]" accesskey="x" href="http://en.wikipedia.org/wiki/Special:Random">Random article</a></li><li title="The place to find out." id="n-help"><a href="http://en.wikipedia.org/wiki/Help:Contents">Help</a></li><li id="n-contact"><a href="http://en.wikipedia.org/wiki/Wikipedia:Contact_us">Contact us</a></li><li title="Support us" id="n-sitesupport"><a href="http://wikimediafoundation.org/wiki/Fundraising">Donations</a></li>	    </ul>
	  </div>
	</div>
		<div id="p-search" class="portlet">
	  <h5><label for="searchInput">Search</label></h5>
	  <div class="pBody">
	    <form action="/wiki/Special:Search" id="searchform"><div>
	      <input id="searchInput" name="search" accesskey="f" value="" type="text">
	      <input name="go" class="searchButton" id="searchGoButton" value="Go" type="submit">&nbsp;<input name="fulltext" class="searchButton" value="Search" type="submit">
	    </div></form>
	  </div>
	</div>
	<div class="portlet" id="p-tb">
	  <h5>Toolbox</h5>
	  <div class="pBody">
	    <ul>
		  		  <li id="t-whatlinkshere"><a title="List of all wiki pages that link here [alt-j]" accesskey="j" href="http://en.wikipedia.org/wiki/Special:Whatlinkshere/Binary_numeral_system">What links here</a></li>
		  		  <li id="t-recentchangeslinked"><a title="Recent changes in pages linked from this page [alt-k]" accesskey="k" href="http://en.wikipedia.org/wiki/Special:Recentchangeslinked/Binary_numeral_system">Related changes</a></li>
		                	      	      	      	      	      	      	      	      	      <li id="t-upload"><a title="Upload images or media files [alt-u]" accesskey="u" href="http://en.wikipedia.org/wiki/Special:Upload">Upload file</a></li>	      	      <li id="t-specialpages"><a title="List of all special pages [alt-q]" accesskey="q" href="http://en.wikipedia.org/wiki/Special:Specialpages">Special pages</a></li>	      	      	      <li id="t-print"><a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;printable=yes">Printable version</a></li>
		  	      	      <li id="t-permalink"><a href="http://en.wikipedia.org/w/index.php?title=Binary_numeral_system&amp;oldid=28277511">Permanent link</a></li>
		  	    </ul>
	  </div>
	</div>
	<div id="p-lang" class="portlet">
	  <h5>In other languages</h5>
	  <div class="pBody">
	    <ul>
	      	      <li class="interwiki-ar">
	      <a href="http://ar.wikipedia.org/wiki/%D9%86%D8%B8%D8%A7%D9%85_%D8%B9%D8%AF_%D8%AB%D9%86%D8%A7%D8%A6%D9%8A">العربية</a>
	      </li>
	      	      <li class="interwiki-ca">
	      <a href="http://ca.wikipedia.org/wiki/Codi_binari">Català</a>
	      </li>
	      	      <li class="interwiki-da">
	      <a href="http://da.wikipedia.org/wiki/Bin%C3%A6re_talsystem">Dansk</a>
	      </li>
	      	      <li class="interwiki-de">
	      <a href="http://de.wikipedia.org/wiki/Dualsystem">Deutsch</a>
	      </li>
	      	      <li class="interwiki-es">
	      <a href="http://es.wikipedia.org/wiki/Sistema_binario">Español</a>
	      </li>
	      	      <li class="interwiki-eo">
	      <a href="http://eo.wikipedia.org/wiki/Duuma_sistemo">Esperanto</a>
	      </li>
	      	      <li class="interwiki-et">
	      <a href="http://et.wikipedia.org/wiki/Kahends%C3%BCsteem">Eesti</a>
	      </li>
	      	      <li class="interwiki-fa">
	      <a href="http://fa.wikipedia.org/wiki/%D8%AF%D8%B3%D8%AA%DA%AF%D8%A7%D9%87_%D8%A7%D8%B9%D8%AF%D8%A7%D8%AF_%D8%AF%D9%88%D8%AF%D9%88%DB%8C%DB%8C">فارسی</a>
	      </li>
	      	      <li class="interwiki-fr">
	      <a href="http://fr.wikipedia.org/wiki/Syst%C3%A8me_binaire">Français</a>
	      </li>
	      	      <li class="interwiki-he">
	      <a href="http://he.wikipedia.org/wiki/%D7%91%D7%A1%D7%99%D7%A1_%D7%91%D7%99%D7%A0%D7%90%D7%A8%D7%99">עברית</a>
	      </li>
	      	      <li class="interwiki-hu">
	      <a href="http://hu.wikipedia.org/wiki/Kettes_sz%C3%A1mrendszer">Magyar</a>
	      </li>
	      	      <li class="interwiki-it">
	      <a href="http://it.wikipedia.org/wiki/Sistema_numerico_binario">Italiano</a>
	      </li>
	      	      <li class="interwiki-nl">
	      <a href="http://nl.wikipedia.org/wiki/Binair">Nederlands</a>
	      </li>
	      	      <li class="interwiki-ja">
	      <a href="http://ja.wikipedia.org/wiki/2%E9%80%B2%E6%95%B0">日本語</a>
	      </li>
	      	      <li class="interwiki-no">
	      <a href="http://no.wikipedia.org/wiki/Bin%C3%A6rtall">Norsk (bokmål)</a>
	      </li>
	      	      <li class="interwiki-pl">
	      <a href="http://pl.wikipedia.org/wiki/Dw%C3%B3jkowy_system_liczbowy">Polski</a>
	      </li>
	      	      <li class="interwiki-ro">
	      <a href="http://ro.wikipedia.org/wiki/Sistem_binar">Română</a>
	      </li>
	      	      <li class="interwiki-fi">
	      <a href="http://fi.wikipedia.org/wiki/Bin%C3%A4%C3%A4rij%C3%A4rjestelm%C3%A4">Suomi</a>
	      </li>
	      	      <li class="interwiki-sl">
	      <a href="http://sl.wikipedia.org/wiki/Dvoji%C5%A1ki_%C5%A1tevilski_sistem">Slovenščina</a>
	      </li>
	      	      <li class="interwiki-sr">
	      <a href="http://sr.wikipedia.org/wiki/%D0%91%D0%B8%D0%BD%D0%B0%D1%80%D0%BD%D0%B8_%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC">Српски / Srpski</a>
	      </li>
	      	      <li class="interwiki-sv">
	      <a href="http://sv.wikipedia.org/wiki/Bin%C3%A4ra_talsystemet">Svenska</a>
	      </li>
	      	      <li class="interwiki-th">
	      <a href="http://th.wikipedia.org/wiki/%E0%B9%80%E0%B8%A5%E0%B8%82%E0%B8%90%E0%B8%B2%E0%B8%99%E0%B8%AA%E0%B8%AD%E0%B8%87">ไทย</a>
	      </li>
	      	      <li class="interwiki-zh">
	      <a href="http://zh.wikipedia.org/wiki/%E4%BA%8C%E8%BF%9B%E5%88%B6">中文</a>
	      </li>
	      	      <li class="interwiki-yi">
	      <a href="http://yi.wikipedia.org/wiki/%D7%91%D7%99%D7%99%D7%A0%D7%A2%D7%A8%D7%99">ייִדיש</a>
	      </li>
	      	      <li class="interwiki-ru">
	      <a href="http://ru.wikipedia.org/wiki/%D0%94%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%B0%D1%8F_%D1%81%D0%B8%D1%81%D1%82%D0%B5%D0%BC%D0%B0_%D1%81%D1%87%D0%B8%D1%81%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F">Русский</a>
	      </li>
	      	    </ul>
	  </div>
	</div>
	      </div><!-- end of the left (by default at least) column -->
      <div class="visualClear"></div>
      <div id="footer">
    <div id="f-poweredbyico"><a href="http://www.mediawiki.org/"><img src="Binary_numeral_system_files/poweredby_mediawiki_88x31.png" alt="MediaWiki"></a></div>	<div id="f-copyrightico"><a href="http://wikimediafoundation.org/"><img src="Binary_numeral_system_files/wikimedia-button.png" alt="Wikimedia Foundation" border="0"></a></div>	<ul id="f-list">
	  <li id="f-lastmod"> This page was last modified 06:47, 14 November 2005.</li>	  	  	  	  <li id="f-copyright">All text is available under the terms of the <a class="internal" href="http://en.wikipedia.org/wiki/Wikipedia:Text_of_the_GNU_Free_Documentation_License">GNU Free Documentation License</a> (see <b><a class="internal" href="http://en.wikipedia.org/wiki/Wikipedia:Copyrights">Copyrights</a></b> for details).<br>
<a class="internal" href="http://wikimediafoundation.org/wiki/Privacy_policy">Privacy policy</a></li>	  <li id="f-about"><a href="http://en.wikipedia.org/wiki/Wikipedia:About" title="Wikipedia:About">About Wikipedia</a></li>	  <li id="f-disclaimer"><a href="http://en.wikipedia.org/wiki/Wikipedia:General_disclaimer" title="Wikipedia:General disclaimer">Disclaimers</a></li>	  	</ul>
      </div>
    <script type="text/javascript"> if (window.runOnloadHook) runOnloadHook(); </script>
    </div>
    <!-- Served by srv24 in 0.05 secs. -->  <script language="JavaScript">
<!--

window.open = SymRealWinOpen;

//-->
</script>

</body></html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Canada Canada
My name is Jiyang Hou (or John Hou). I was born in HeiLongJiang province in north east of China. I got all my educations in China. My university major is Geophysics, but my main professional role is software developer. My biggest accomplishment so far is quit smoking about 5 years ago after almost 20 years smoking history. I am still interested on programming beside making living with it like many other developers. I immigrated to Canada in 2003 and became a permanent resident till now. I live in Calgary, Alberta, Canada. You can reach me by jyhou69@gmail.com regarding to any questions, comments, advice, etc.

Comments and Discussions