You are on page 1of 13

Computer Arithmetic

Read the material below and follow the instructions at points 7 to 10. Then answer the questions at point 12.

1 2 3 4

5

6

7

Computer hardware can perform arithmetic just by blindly following rules. The rules are very simple to implement if the numbers use a binary representation. The two possible values 0 and 1 are distinguished by having different physical measurements associated with them, such as voltage but could equally well be represented by two colours, or people standing around holding flags up or down. The first rule describes how two single binary digits (bits) are added: The result is a sum bit and a carry bit, just like adding two decimal digits, such as 6 + 8 = 4 with carry 1 A device that can sum two bits is called a half adder The second rule applies when two k -bit numbers are to be added: The carry-out from one sum must be fed to the next addition as a carry-in Thus there are three inputs (A, B, carry-in) and two outputs (sum and carry-out) A device that can sum two bits plus carry-in is called a full adder To add two k-bit numbers we need a chain of k -1 full adders and a half adder (or k full adders) Each carry-out is connected to the next stage's carry-in, with a final carry out Hardware uses more primitive components (AND, OR and exclusive-OR gates) to implement the truth tables. How gates work (switched lights analogy): http://plus.maths.org/issue36/features/nishiyama/ Building half and full adders from gates: http://en.wikipedia.org/wiki/Adder_(electronics) Here are the truth tables that define binary addition (and ultimately, subtraction, multiplication, division, …). Your task is to complete the definitions by filling in the shaded cells of both tables. Half Adder Truth Table Inputs Outputs Full Adder Truth Table Inputs Outputs

A
0 0 1 1

B
0 1 0 1

C out
0 0 0 1

S
0 1 1 0 Complete the table by entering the appropriate value in each shaded cell

A
0 0 1 1 0 0 1 1

B
0 1 0 1 0 1 0 1

C in
0 0 0 0 1 1 1 1

C out
0 0 0 1 0 1 1 1

S
0 1 1 0 1 0 0 1

S = (A + B ) modulo 2 = A  B (exclusive-or) C out = 1 if A and B are both 1

An Adding Machine 8 We have built an 8-bit adding machine that uses the truth tables you've completed in step 7 to make all its decisions. Whether it works or not will depend on the correctness of the tables! Enter two values between 0 and 255 in boxes A and B. Tab out and press Convert to obtain the binary equivalents. Press Connect to see how the adders are connected up. Press Calculate to see the data flow through the adders (adjust the speed to suit).

9 10

Convert

Connect

Calculate

(most significant bit)

A B 127 127 = = 0 0 1 1 1 1 1 1 1 1 1 1 0 Cout S 1 Cout S 1 Cout S 1 Cout S 1 Cout S 1 Cout S 1 C Result 254 = 1 1 1 1 1 1 11 Note how each adder has to wait until the one to its right has produced a carry before it can produce its own sum and carry. What happens to the bit pattern of a number when you double it? The bit pattern of a doubled number shifts one digit to the left. B How do you interpret an answer where a carry-out occurs? 12 The carry out from one sum must be fed to the next C D For what two numbers will all the carry bits be 1. Y = X /2 = What is Y 's bit pattern (using the converter)? 85 Add this number (Y ) to itself using the machine. This limitation is resolved in practice by lookahead carry logic (not essential knowledge). what number is the sum of the first 4 odd powers of 2? X = 21 + 23 + 25 + 27 = What is exactly half X. except for the final carry out? 127 What would you have to change to make a 32-bit adder out of the copies of 8-bit adder shown above? Connect four 8-bit adders together and link the final carry-out to the other adders E How could you make a full adder out of two half adders and a device that implements the OR function (hard)? Inserting a carry-in input to the ciruit . Try to answer these questions (your tutor will check) A In decimal.

ENGG1811 Lab Week 1 urements associated with them. such as 6 + 8 = 4 with carry 1 mplement the truth tables. such as voltages. Complete the table by entering the appropriate value in each shaded cell S = (A + B + C in ) modulo 2 C out = 1 if more than one of A. B or C in are equal to 1 n step 7 to make all its decisions. Calculate Speed 10 (least significant bit) . to obtain the binary equivalents.

170 zero-one-zero-one-zero-one-zero-one ttern of a number when you double it? rry out from one sum must be fed to the next addition using a carry-in 127 8-bit adder shown above? mplements the OR function (hard)? .1 1 1 1 1 Cout S 1 Cout S 1 0 e it can produce its own sum and carry.

0034 Uses simplest representation: integer.78200340000000000E+03 Preset number of significant figures (say 18) The shaded cells below display numbers with 18 figures The tan shaded cells are for you to enter values or formulas as instructed Only simple formulas will be used. ± infinity and NaN (not a number) MS Excel can display numbers in several ways. only an approximation Excel stores real number approximations in a form of scientific notation Instead of a decimal form such as 1.cs. Take -3782.Representation of Real Numbers Read the five short sections below and complete the exercises where noted. an equals sign followed by an expression with two operands 2 Representation limits Largest magnitude(approx) 9.html Most common representation is double precision (64 bits in all) 52 bits for the normalised fraction 52 x log10 2 ~ 16 decimal digits of precision 210 gives a magnitude range of ~ 10308 11 bits for the exponent 1 sign bit Special values for zero. your tutor will ask you some questions such as those in section 6.00 Preset number of decimal places (say 2) Scientific -3.qc.99999999999999000E-307 Try entering 1e-309 These are imposed by Excel.wikipedia. the IEEE 754 limits are slightly different Answer the questions on this topic at the bottom of the sheet 3 Multiplication and Division Multiplying or dividing numbers maintains accuracy unless overflow or underflow occurs Overflow magnitude is greater than rep limit Try =1e300*1e9 Try =1e200/1e-109 Underflow magnitude is smaller than rep limit Try =1e-210*1e-99 Try =1e-150/1e159 . 1 Real Number Representation Real numbers in general cannot be represented exactly.463 x 10-24 a binary format is used on the same principle The IEEE 754 standard is supported by most computer hardware References: http://en.99999999999999000E+307 Try entering 2e+308 (for 2 x 10308) Smallest non-zero magnitude 9. fixed or scientific Fixed decimal places -3782. When you have done this.org/wiki/IEEE_floating-point_standard http://babbage.0034 for example General format -3782.edu/IEEE-754/Decimal.

00E+50 9.23456798 -9.2 9.9999999999999 1.223 4.00000000000000000E+00 4.99999999999996000E-02 1. enter pairs numbers that differ by a small fraction of their magnitude 0.5 1.00031094518727000E-13 1 0.05 0.Operand2 Operand 1 Operand 2 1.3 2.4 1.50946000000000000E-23 Enter the value 0.999999999999 9.69E+51 1.00000000000000000E+00 1.00000000000001000E+10 1.123 1.01 4.00000001191614000E-08 1 0.999999999999999 0.79579000000000000E+02 1.00002230000000000E+05 Adding numbers of vastly different magnitudes loses accuracy as the smaller value gets "lost" in the larger Operand 1 10000000000 10000000000 10000000000 5 Subtraction Subtracting numbers of vastly different magnitudes is just like addition: it loses accuracy Unlike addition.00000000000000000E-01 2.00000000000000000E+00 In the boxes below.00000000000000000E-02 Operand 2 1 0.456 1.00000000000000000E+10 Although one subtraction may not make a big difference.33333333333333000E-01 (notice the precision limit) Sometimes the rounding doesn't compensate: Enter the value 40000.00000000010000000E+10 1.0001 0.Although many simple decimal values can't be represented exactly.00000000000000000E-01 3.79000000000000000E+51 1.00002229999999000E+04 4 Addition Adding numbers of similar magnitudes and same sign generally maintains accuracy Operand 1 -123.23456789 1.25 -5.00001 Operand1 + Operand2 1.50E-23 Operand1 + Operand2 -5. hardware rounding allows them to obey exact rules of arithmetic in 1.46E-26 Operand 2 -456.1 Enter the formula =1/3 1.99977878279878000E-13 1 0.2 0. some calculations may apply these operations many times and the errors acc 6 A B C D Questions your tutor may ask you What happens if you enter a value whose magnitude is greater than the representation limit? What happens if you enter a value whose magnitude is smaller than can be represented? What happens if an Excel calculation results in overflow? What happens if an Excel calculation results in underflow? .00000000000000000E-02 0. subtracting numbers of similar magnitude and same sign also loses accuracy This is a sometimes unexpected source of error in scientific and engineering calculations Operand1 .

E What are the different ways Excel shows that the result of a calculation is invalid? .

wikimedia. fixed or scientific r of decimal places (say 2) r of significant figures (say 18) 2e+308 0.00000000000000000E+00 0. Shows the rep for any decimal value http://upload.00000000000000000E+00 is this a number or a string? #NUM! #NUM! 0.svg decimal digits of precision gnitude range of ~ 10308 representation: integer.00000000000000000E+00 .ENGG1811 Lab Week 1 ons such as those in section 6.org/wikipedia/commons/a/a9/IEEE_754_Double_Floating_Point_Format.

1 This matters sometimes Not quite -9e-08 Result has only about 5 significant figures Result has only about 4 significant figures Result has no significant figures How many significant figures? 1 1 hese operations many times and the errors accumulate The output becomes a string.1 as expected Not quite 0.allows them to obey exact rules of arithmetic in most cases Multiplies cell contents by (exactly) 10 Multiplies cell contents by (exactly) 3 Multiplies cell contents by (exactly) 10 Answer is correct Answer is correct Answer is correct Answer is correct Answer is correct Lost operand 2 Exactly 0. The output becomes a long number consisting of only zeros to the power of zero A '#NUM!' message is the output The output becomes a long number consisting of only zeros to the power of zero .

It displays abbreviated error messages such as "#DIV/0!" when dividing by zero and so on or the less obvious ac .

.

.

iding by zero and so on or the less obvious accuracy losses .