<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m0521">

  <name>Binary notation</name>

  <metadata>
  <md:version>2.6</md:version>
  <md:created>2000/08/08</md:created>
  <md:revised>2004/08/04 15:30:07 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="mrshawn">
      <md:firstname>Shawn</md:firstname>
      
      <md:surname>Stewart</md:surname>
      <md:email>mrshawn@alumni.rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>base 2</md:keyword>
    <md:keyword>binary</md:keyword>
    <md:keyword>bit</md:keyword>
  </md:keywordlist>

  <md:abstract>A short course on our friend, Base 2.
</md:abstract>
</metadata>

  <content>

    <para id="p1">
      We need to concentrate on how computers represent numbers and
      perform arithmetic.  Suppose we have twenty-six "things."  This
      number is an abstract quantity that we need to represent so that
      we can perform calculations on it.  We represent this number
      with <term>positional notation</term> using base 10:
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="10">26</m:cn>
	  <m:apply>
	    <m:plus/>
	    <m:cn type="e-notation">2<m:sep/>1</m:cn>
	    <m:cn type="e-notation">6<m:sep/>0</m:cn>
	  </m:apply>
	</m:apply>
      </m:math>
      .  We could also use base-8 notation ( 
      <m:math> 
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="8">32</m:cn>
	  <m:apply>
	    <m:plus/>
	    <m:apply>
	      <m:times/>
	      <m:cn>3</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>8</m:cn>
		<m:cn>1</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn>2</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>8</m:cn>
		<m:cn>0</m:cn>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:apply>
      </m:math>
      ), base 16 ( 
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="16">1A</m:cn>
	  <m:apply>
	    <m:plus/>
	    <m:apply>
	      <m:times/>
	      <m:cn>1</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>16</m:cn>
		<m:cn>1</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn type="integer" base="16">A</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>16</m:cn>
		<m:cn>0</m:cn>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:apply>
      </m:math>
      , with <m:math><m:cn type="integer" base="16">A</m:cn></m:math>
      representing ten), or base 2 (
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="2">11010</m:cn>
	  <m:apply>
	    <m:plus/>
	    <m:apply>
	      <m:times/>
	      <m:cn>1</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>4</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn>1</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>3</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn>0</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>2</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn>1</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>1</m:cn>
	      </m:apply>
	    </m:apply>
	    <m:apply>
	      <m:times/>
	      <m:cn>0</m:cn>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>0</m:cn>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:apply>
      </m:math>
      ).  Each of these represent the <emphasis>same</emphasis>
      quantity; all numbers can be so represented and arithmetic can
      be performed in each system.  Which we chose is a matter of
      convenience and cultural heritage.  Each position is occupied by
      an integer between 0 and the base minus 1, and the integer's
      position implicitly represents how many times the base raised to
      an exponent is needed.  The integer
      <m:math>
	<m:ci>N</m:ci>
      </m:math> 
      is succinctly expressed in base-b as 
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="b">N</m:cn>
	  <m:apply>
	    <m:times/>
	    <m:ci>
	      <m:msub>
		<m:mi>d</m:mi>
		<m:mi>n</m:mi>
	      </m:msub>
	    </m:ci>
	    <m:ci>
	      <m:msub>
		<m:mi>d</m:mi>
		<m:mrow>
		  <m:mi>n</m:mi>
		  <m:mo>−</m:mo>
		  <m:mn>1</m:mn>
		</m:mrow>
	      </m:msub>
	    </m:ci>
	    <m:ci>…</m:ci>
	    <m:ci>
	      <m:msub>
		<m:mi>d</m:mi>
		<m:mn>0</m:mn>
	      </m:msub>
	    </m:ci>  
	  </m:apply>
	</m:apply>  
      </m:math>
      , which mathematically means 
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:cn type="integer" base="b">N</m:cn>
	  <m:apply>
	    <m:plus/>
	    <m:apply>
	      <m:times/>
	      <m:ci>
		<m:msub>
		  <m:mi>d</m:mi>
		  <m:mi>n</m:mi>
		</m:msub>
	      </m:ci>
	      <m:apply>
		<m:power/>
		<m:ci>b</m:ci>
		<m:ci>n</m:ci>
	      </m:apply>
	    </m:apply>
	    <m:ci>…</m:ci>
	    <m:apply>
	      <m:times/>
	      <m:ci>
		<m:msub>
		  <m:mi>d</m:mi>
		  <m:mn>0</m:mn>
		</m:msub>
	      </m:ci>
	      <m:apply>
		<m:power/>
		<m:ci>b</m:ci>
		<m:cn>0</m:cn>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:apply>
      </m:math>
      .  No matter what base you might choose, addition, subtraction,
      multiplication, and division all follow the same rules you
      learned as a child.  These rules can be derived mathematically
      from the positional notation conventions defined by this
      formula.  (Non-positional systems are very strange; take Roman
      numerals for example.  Try adding (or, more extremely, dividing)
      two numbers using this number representation system).  To extend
      the positional representation to signed integers, we add a
      symbol that represents whether the number is positive or
      negative.
    </para>

    <para id="p2">
      Humans have ten fingers, and so it's not so surprising that many
      cultures throughout history have used base 10.  Digital
      computers use base 2 or <term>binary</term> number
      representation, each digit of which is known as a
      <term>bit</term> (<emphasis>b</emphasis>inary dig
      <emphasis>it</emphasis>).  Here, each bit is represented as a
      voltage that is either "high" or "low," thereby representing "1"
      or "0", respectively.  To represent signed values, we tack on a
      special bit — the <term>sign bit</term> — to express
      the sign.  The binary addition and multiplication tables are

      <equation id="eqn0000">
	<m:math>
	  <m:apply>
	    <m:eq/>
	    <m:apply>
	      <m:plus/>
	      <m:cn>0</m:cn>
	      <m:cn>0</m:cn>
	    </m:apply>
	    <m:cn>0</m:cn>
	  </m:apply>
	</m:math>
      </equation>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:plus/>
	    <m:cn>1</m:cn>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:cn>10</m:cn>
	</m:apply>
      </m:math>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:plus/>
	    <m:cn>0</m:cn>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:cn>1</m:cn>
	</m:apply>
      </m:math>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:plus/>
	    <m:cn>1</m:cn>
	    <m:cn>0</m:cn>
	  </m:apply>
	  <m:cn>1</m:cn>
	</m:apply>
      </m:math>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:times/>
	    <m:cn>0</m:cn>
	    <m:cn>0</m:cn>
	  </m:apply>
	  <m:cn>0</m:cn>
	</m:apply>
      </m:math>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:times/>
	    <m:cn>0</m:cn>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:cn>0</m:cn>
	</m:apply>
      </m:math>
      
      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:times/>
	    <m:cn>1</m:cn>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:cn>1</m:cn>
	</m:apply>
      </m:math>

      <m:math display="block">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:times/>
	    <m:cn>1</m:cn>
	    <m:cn>0</m:cn>
	  </m:apply>
	  <m:cn>0</m:cn>
	</m:apply>
      </m:math>

    </para>

    <para id="p3">
      A <term>carry</term> means that a computation performed at a
      given position affects other positions as well.  Here,
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:plus/>
	    <m:cn type="integer" base="2">1</m:cn>
	    <m:cn type="integer" base="2">1</m:cn>
	  </m:apply>
	  <m:cn type="integer" base="2">10</m:cn>
	</m:apply>
      </m:math>
      is an example of a computation that involves a carry.  Note that
      if carries are ignored, subtraction of two single-digit binary
      numbers yields the same bit as addition.  Computers use high and
      low voltage values to express a bit, and an array of such
      voltages express numbers akin to positional notation.  Logic
      circuits perform arithmetic operations.
    </para>

    <exercise id="exer1">
      
      <problem>
	<para id="prob">
	  Add twenty-five and seven in base 2.  Note the carries that
	  might occur.  Why is the result "nice"?
	</para>
      </problem>
      
      <solution>
	<para id="sol">
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:cn type="integer" base="10">25</m:cn>
	      <m:cn type="integer" base="2">11001</m:cn>
	    </m:apply>
	  </m:math>
	  and
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:cn type="integer" base="10">7</m:cn>
	      <m:cn type="integer" base="2">111</m:cn>
	    </m:apply>
	  </m:math>
	  .  We find that
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:plus/>
		<m:cn type="integer" base="2">11001</m:cn>
		<m:cn type="integer" base="2">111</m:cn>
	      </m:apply>
	      <m:cn type="integer" base="2">100000</m:cn>
	      <m:cn type="integer" base="10">32</m:cn>
	    </m:apply>
	  </m:math>
	  .
	</para>
      </solution>
    </exercise>

  </content>
</document>
