Wednesday, December 8, 2010

Computer Arithmetic

Computer Arithmetic

Because electronic logic deals with currents that are on or off, it has been found convenient to represent quantities in binary form to perform arithmetic on a computer. Thus, instead of having ten different digits, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, in binary arithmetic, there are only two different digits, 0 and 1, and when moving to the next column, instead of the digit representing a quantity that is ten times as large, it only represents a quantity that is two times as large. Thus, the first few numbers are written in binary as follows:
Decimal   Binary
Zero        0        0
One         1        1
Two         2       10
Three       3       11
Four        4      100
Five        5      101
Six         6      110
Seven       7      111
Eight       8     1000
Nine        9     1001
Ten        10     1010
Eleven     11     1011
Twelve     12     1100
The addition and multiplication tables for binary arithmetic are very small, and this makes it possible to use logic circuits to build binary adders.
+ | 0  1       * | 0  1
---------      ---------
0 | 0  1       0 | 0  0
1 | 1 10       1 | 0  1
Thus, from the table above, when two binary digits, A and B are added, the carry bit is simply (A AND B), while the last digit of the sum is more complicated; ((A AND NOT B) OR ((NOT A) AND B)) is one way to express it.
And multiplication becomes a series of shifts, accompanied by successive decisons of whether to add or not to add.
When a number like 127 is written in the decimal place-value system of notation, one can think of it as indicating that one has:
seven marbles,
two boxes that have ten marbles in each box,
one box that has ten boxes each with ten marbles in that box.
The binary system is another place-value system, based on the same principle; however, in that system, the smallest boxes only have two marbles in them, and any larger box contains only two of the next smaller size of box.
We have seen how we can use two digits, 0 and 1, to do the same thing that we can do with the digits 0 and 9 only; write integers equal to or greater than zero. In writing, it is easy enough to add a minus sign to the front of a number, or insert a decimal point. When a number is represented only as a string of bits, with no other symbols, special conventions must be adopted to represent a number that is negative, or one with a radix point indicating a binary fraction.
Historically, computers have represented negative numbers in several different ways.
One method is sign-magnitude representation, in which the first bit of the integer, if a one, indicates that the number is negative.
Another is one's complement notation, in which, when a number is negative, in addition to this being indicated by setting the first bit of the number to a one, the other bits of the number are all inverted.
By far the most common way to represent negative integers on modern computers, however, is two's complement notation, because in this notation, addition of signed quantities, except for the possible presence of a carry out when an overflow is not actually taking place, is identical to the normal addition of positive quantities. Here, to replace a number with its negative equivalent, one inverts all the bits, and then adds one.
Another method of representing negative numbers is simply to add a constant, equal to half the range of the integer type, to the number to obtain its binary representation. This is usually used for the exponent portion of floating-point numbers, as we will see later.
Number  Binary representations                      Decimal representations

        Sign-      One's      Two's      Excess-    Sign-     Nine's     Ten's      Excess-
        magnitude  complement complement 512        magnitude complement complement 500

+511    0111111111 0111111111 0111111111 1111111111
+510    0111111110 0111111110 0111111110 1111111110
...
+500    0111110100 0111110100 0111110100 1111110100
+499    0111110011 0111110011 0111110011 1111110011 499       499        499        999
+498    0111110010 0111110010 0111110010 1111110010 498       498        498        998
...
+2      0000000010 0000000010 0000000010 1000000010 002       002        002        502
+1      0000000001 0000000001 0000000001 1000000001 001       001        001        501
 0      0000000000 0000000000 0000000000 1000000000 000       000        000        500
-1      1000000001 1111111110 1111111111 0111111111 501       998        999        499
-2      1000000010 1111111101 1111111110 0111111110 502       997        998        498
...
-498    1111110010 1000001101 1000001110 0000001110 998       501        502        002
-499    1111110011 1000001100 1000001101 0000001101 999       500        501        001
-500    1111110100 1000001011 1000001100 0000001100                      500        000
...
-510    1111111110 1000000001 1000000010 0000000010
-511    1111111111 1000000000 1000000001 0000000001
-512                          1000000000 0000000000
Floating-point numbers are used in scientific calculations. In these calculations, instead of dealing in numbers which must always be exact in terms of a whole unit, whether that unit is a dollar or a cent, a certain number of digits of precision is sought for a quantity that might be very small or very large.
Thus, floating-point numbers are represented internally in a computer in something resembling scientific notation.
In scientific notation,
6
1,230,000  becomes 1.23 * 10
and the common logarithm of 1,230,000 is 6.0899; the integer and fractional parts of a common logarithm are called, respectively, the characteristic and the mantissa when using common logarithms to perform calculations.
To keep the fields in a floating-point number distinct, and to make floating-point arithmetic simple, a common way to represent floating point numbers is like this:
First, the sign of the number; 0 if positive, 1 if negative.
Then the exponent. If the exponent is seven bits long, it is represented in excess-64 notation; if it is eleven bits long, as in this example, it is represented in excess-1,024 notation. That is, the bits used for the exponent are the binary representation of the exponent value plus 1,024; this is the simplest way to represent exponents, since everything is now positive.
Finally, the leading digits of the actual binary number. As these contain the same information as the fractional part of the base-2 logarithm (or the base-16 logarithm, had the exponent been a power of 16, as is the case on some other architectures instead of the one examined here) of the number, this part of a floating-point number is also called the mantissa, even if this term is much criticized as a misnomer.
The exponent is in excess 1,024 notation when the binary point of the mantissa is considered to precede its leading digit.
As the digits of the mantissa are not altered when the number is negative, sign-magnitude notation may be said to be what is used for that part of the number.
Some examples of floating-point numbers are shown below:
1,024          0 10000001011 10000
  512          0 10000001010 10000
  256          0 10000001001 10000
  128          0 10000001000 10000
   64          0 10000000111 10000
   32          0 10000000110 10000
   16          0 10000000101 10000

   14          0 10000000100 11100
   12          0 10000000100 11000
   10          0 10000000100 10100

    8          0 10000000100 10000

    7          0 10000000011 11100
    6          0 10000000011 11000
    5          0 10000000011 10100

    4          0 10000000011 10000

    3.5        0 10000000010 11100
    3          0 10000000010 11000
    2.5        0 10000000010 10100

    2          0 10000000010 10000

    1.75       0 10000000001 11100
    1.5        0 10000000001 11000
    1.25       0 10000000001 10100

    1          0 10000000001 10000

    0.875      0 10000000000 11100
    0.75       0 10000000000 11000
    0.625      0 10000000000 10100

    0.5        0 10000000000 10000
    0.25       0 01111111111 10000
    0.125      0 01111111110 10000
    0.0625     0 01111111101 10000
    0.03125    0 01111111100 10000

    0          0 00000000000 00000

   -1          1 10000000001 10000
In each of the entries above, the entire 11-bit exponent field is shown, but only the first five bits of the mantissa field are shown, the rest being zero.
Note that for all the numbers except zero, the first bit of the mantissa is a one. This is somewhat wasteful; if the exponent is a power of 4, 8, or 16 instead of a power of two, then the restriction seen will only be that the first 2, 3, or 4 bits, respectively, of the mantissa will not all be zero.
A floating-point number whose first digit, where the size of a digit is determined by the base of the exponent, is not zero is said to be normalized. In general, when a floating-point number is normalized, it retains the maximum possible precision, and normalizing the result is an intrinsic part of any floating-point operation.
In the illustration above,
0 00000000000 10000
would represent the smallest number that can possibly be normalized. Some computers permit gradual underflow, where quantities such as
0 00000000000 01000
are also allowed, since they are as normalized as is possible, and their meaning is unambiguous.

The Early Days of Hexadecimal

I don't know where else on my site to put this amusing bit of trivia.
Most computers, internally, use binary numbers for arithmetic, as circuits for binary arithmetic are the simplest to implement. Some computers will perform base-10 arithmetic instead, so as to directly calculate on numbers as they are used by people in writing. This is usually done by representing each digit in binary form, although a number of different codings for decimal digits have been used.
When a computer uses binary arithmetic, it is desirable to have a short way to represent binary numbers. One way to do this is to use octal notation, where the digits from 0 to 7 represent three consecutive bits; thus, integers in base-8 representation can be quickly translated to integers in base-2 representation. Another way, more suited to computers with word lengths that are multiples of four digits, is hexadecimal notation.
Today, programmers are familiar with the use of the letters A through F to represent the values 10 through 15 as single hexadecimal digits. Before the IBM System/360 made this the universal standard, some early computers used alternate notations for hexadecimal:
10 11 12 13 14 15
System/360         A  B  C  D  E  F
C                  a  b  c  d  e  f
SWAC               u  v  w  x  y  z
Monrobot XI        S  T  U  V  W  X
Datamatic D-1000   b  c  d  e  f  g
LGP-30             f  g  j  k  q  w
ILLIAC             k  s  n  j  f  l

No comments:

Post a Comment