Experiment 0- Introduction to Binary Numbers and Related Number Systems

Everything in a computer is stored as binary numbers including programs, data, music, video, email, etc.  For this reason, we need a basic understanding of the binary number system.

Positional number systems are simplest and easiest to use.  The decimal number system is positional; that is, the value of each digit depends on its position relative to the decimal point.  For a whole number, the position is relative to the right most digit where the decimal is understood to be.  The position value is related to a power of the number system base, in this case, 10.  The position values beginning with the right most digit and moving left are 100 =1; 101=10; 102=100; 103=1000; and so forth.  The place value at a position is the position value multiplied by the digit at that position.  The number itself can be represented by the sum of its place values.  For instance, 125 can be represented as 1*100 + 2*10 + 5*1.  The number zero is crucial in positional number systems because it holds a position without contributing to the place value.  Take the decimal number 107.  Its value is 1*100 +  0 * 10 + 7 * 1.  Without the zero to hold the "place", the value of the number "1" would be ambiguous.

Another characteristic of positional number systems is that there are the same number of symbols as the base number.  As humans, we are relatively free to choose the base and then make up the necessary number of symbols.  Somewhere along the line we chose base 10 and adopted 10 symbols (digits) 0, 1, 2, ..., 8, 9.  But where digital computers are concerned, we don't have a choice because they have only two "states": "off" and "on".  Thus, we are forced to choose base 2 and use the binary number system.  As for symbols, the logical thing to do is use "0" and "1" to represent "off" and "on" respectively.

To store the number 94 in a computer we must convert it to binary ones and zeros. Here's one way to do it:  Divide the number by 2.  If it divides evenly with no remainder, write down the binary digit 0. If it does not, write down the binary digit 1.  Using the quotient, repeat the process writing down the calculated "0" or "1" each time.  When done, reverse the order of 1 and 0 digits.  For 94, we have:

        94/2 = 47  even        0
        47/2 = 23 not even   1
        23/2 = 11 not even   1
        11/2 = 5   not even   1
        5/2 = 2     not even   1
        2/2 = 1     even         0
        1/2 = 0     not even   1

Writing down the reverse we have 1011110.  To check the result we convert back to decimal as follows 1*26 + 0*25 +1*24+1*23 + 1*22 + 1*21 * 0*20 =  64 + 0 + 16 + 8 + 4 + 2 + 0 = 94. 

Fortunately, conversion can be readily handled by a computer program, so we can work with numbers in decimal and let the computer work in binary.

Arithmetic with binary numbers is essentially the same as with decimals.  Take addition for instance.  When adding a column of binary numbers, a sum greater than 1 produces a "carry" to the next column.  Consider the example below.
       1111    (CARRY)
    101101     45
 +   10111   +23
  1000100     68
In the first column, 1 + 1 = 10 (not 2), so we write down a zero and carry a one.  In the second column, the carry plus 1 yields the same again.  In the third column, we have 1 + 1 + 1 = 11.  We write down 1 this time and carry a one.  So it goes for the remainder of the columns.

Subtraction with binary numbers works much the same as with decimals. The concept of "borrowing" is exactly the same. 
          11    (BORROW)
    101101     45
 -    10111   -23
      10110      22
In the second column, 1 cannot subtract 1 from 0, so we borrow 1 from the third minuend digit.  We now have 10 - 1 = 1 for the second column.  The zero now in the third column requires a "borrow" from the fourth column.  Continuing we get the answer.

Besides representing numbers, there are other applications for binary representation in computers.  For instance, when configuring computer peripheral devices, individual bits or groups of bits can represent certain options. In another instance, a bit input to the computer might represent a car door being either open.  In both cases, we will have to work with binary representations so that bits can be accessed and possibly changed.

Computers can handle these as well as other binary operations with great speed.  We need only designate the operation and the values to be operated on.  That's where programming comes in.  More about that later.

Computers store binary values in "registers" of fixed bit width.  From early days, designers chose to make these registers 8 bits or a multiple of 8 bits wide.  The Intel 8080's working and addressable memory registers are 8 bits wide.  More recent microprocessors use 32 or even 64 bit wide registers.  An 8-bit wide register can store binary values as small as 0 (00000000) or as large as 255 (11111111).  This represents 256 different values, one more than the maximum value.  A 16-bit register can store values from 0 (0000000000000000) to 65,535 (1111111111111111).  The address register in the Intel 8080 is 16 bits, so its address space includes 65,535 + 1 = 65,536 different 8-bit memory registers.  By linking 8-bit registers together, numbers as large as desired can be represented.

Long strings of 1s and 0s are difficult to work with.  A way around this is to use octal (base 8) or hexadecimal (base 16) representations that show binary numbers in more compact form.  "Hexadecimal" (base 16) or "hex" for short is the most widely used.  Hex has sixteen symbols: 0, 1, 2, ... , 9, A, B, C, D, E, F.  A through F represent values 10 through 15.  To represent an eight bit binary value like 10100111 in hex, split it into two four bit groups: 1010 0111.  Now, rewrite each group with its hex equivalent: 1010 0111 = A7 (1010 = A and 0111 = 7).  The range of the Intel 8080 address space above in binary becomes much more manageable in hex as 0000 to FFFF.

"Octal" base 8 is much less frequently used.  The eight associated symbols are 0, 1, 2, 3, 4, 5, 6, 7.  An 8-bit value like 01011110 would be written 3-bit groups as 01 011 110 then each group rewritten as octal yielding 136.  The address range of an Intel 8080 would be 000000 to 177777.  Usually the 16-bit address is first spilt into two 8-bit groups producing split octal representation:  000 000 to 377 377.  Octal is rarely used, however it does come in handy when working with Intel 8080 machine language.

Using number systems other than decimal may seem difficult at first, but stick with it a while and it will become as natural as decimal.

To learn about binary, check out the Khan Academy

Continue to next Experiment - Click Here