Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.

Similar presentations


Presentation on theme: "Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi."— Presentation transcript:

1 Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi

2 Digital Number Systems Digital number systems have a base or radix b Using positional notation, an m-digit base b number is written x = x m-1 x m-2... x 1 x 0 0  x i  b-1, 0  i < m The value of this unsigned integer is

3 Range of Unsigned m Digit Base b Numbers The largest number has all of its digits equal to b-1, the largest possible base b digit Its value can be calculated in closed form An important summation—geometric series

4 Number Systems base 10 (decimal) 10 digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 positional notation 1011 ten = 1 x 10 3 + 0 x 10 2 + 1 x 10 1 + 1 x 10 0 base 2 (binary) 2 digits 0,1 1011 two = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 1011 two = 11 ten

5 Number Systems base 8 (octal) 8 digits 0, 1, 2, 3, 4, 5, 6, 7 1001 eight = 1 x 8 3 + 0 x 8 2 + 1 x 8 1 + 1 x 8 0 1001 eight = 521 ten base 16 (hexadecimal) 16 digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 1001 sixteen = 1 x 16 3 + 0 x 16 2 + 1 x 16 1 + 1 x 16 0 1001 sixteen = 4113 ten

6 Number Systems: 2 The digital logic of computers: simple, reliable circuits (AND, OR and NOT gates) that “lock” one of two states (On / Off, True / False, 0 / 1) To represent numbers, a single binary digit (bit) can represent 2 possible numbers (0 or 1). We can add more bits to the number giving the additional bits a place value, just as we do for decimal numbers (base 10). Each additional bit doubles the number of possible combinations:

7

8 To convert a binary number to decimal, one just adds up the place values of the 1-bits, as follows : Bit position Binary number 7 6 5 4 3 2 1 0 1 1 0 1 1 4 8 16 64 128 ------- 221 You can try with 10100111 The number has 8 bits I numbered starting with 0 on the right--> the place value of each bit is 2 to the power of the bit number EX: 2 to the 7th = 128

9 A story: Seven computer scientists went for a walk. After a while they counted themselves: 0, 1, 2, 3, 4, 5, 6. One must be missing! So they spent the rest of the day looking, but never figured out where the 7th one could have gone. This is known as an “off by one” error. It is all too common.

10 To convert from decimal to binary: divide by 2 repeatedly until 0 is reached then the remainders, reading up, are the binary number 221 /2 = 110 r = 1 110 / 2 = 55r = 0 55 / 2 = 27r = 1 27 / 2 = 13r = 1 13 / 2 = 6r = 1 6 / 2 = 3r = 0 3 / 2 = 1 r = 1 1 / 2 = 0 r = 1 Answer: 11011101

11 Radix Conversion: General Matters Converting from one number system to another involves computation We call the base in which calculation is done c and the other base b Calculation is based on the division algorithm — For integers a and b, there exist integers q and r such that a = q  b + r, with 0  r  b-1 Notation: q =  a/b  r = a mod b

12 Digit Symbol Correspondence Between Bases Each base has b (or c) different symbols to represent the digits If b < c, there is a table of b + 1 entries giving base c symbols for each base b symbol and b If the same symbol is used for the first b base c digits as for the base b digits, the table is implicit If c < b, there is a table of b + 1 entries giving a base c number for each base b symbol and b For base b digits  c, the base c numbers have more than one digit Base 12: 0 1 2 3 4 5 6 7 8 9 A B 10 Base 3: 0 1 2 10 11 12 20 21 22 100 101 102 110

13 Convert Base b Integer to Calculator’s Base, c 1) Start with base b x = x m-1 x m-2... x 1 x 0 2) Set x = 0 in base c 3) Left to right, get next symbol x i 4) Lookup base c number D i for symbol x i 5) Calculate in base c: x = x  b + D i 6) If there are more digits, repeat from step 3 Example: convert 3AF 16 to base 10 x = 0 x = 16 x + 3 = 3 x = 16  3 + 10(= A) = 58 x  16  58 + 15(= F) = 943

14 Convert Calculator’s Base Integer to Base b 1) Let x be the base c integer 2) Initialize i = 0 and v = x & get digits right to left 3) Set D i = v mod b & v =  v/b . Lookup D i to get x i 4) i = i + 1; If v  0, repeat from step 3 Example: convert 3567 10 to base 12 3587  12 = 298 (rem = 11)  x 0 = B 298  12 = 24 (rem = 10)  x 1 = A 24  12 = 2 (rem = 0)  x 2 = 0 2  12 = 0 (rem = 2)  x 3 = 2 Thus 3587 10 = 20AB 12

15 Negative Numbers need a representation for negative numbers use two’s complement binary number negated as follows starting from rightmost bit and working left, copy up to and including the first 1-bit; thereafter, write 1-bits for 0-bits and 0-bits for 1-bits. example in 16-bit word 42 0000 0000 0010 1010 -42 1111 1111 1101 0110

16 Complement Number Systems Complement number systems use unsigned numbers to represent both positive and negative numbers Recall that the range of an m digit base b unsigned number is 0  x  b m -1 The first half of the range is used for positive, and the second half for negative, numbers Positive numbers are simply represented by the unsigned number corresponding to their absolute value

17 Complement Operations for m-Digit Base b Numbers Radix complement of m-digit base b number x x c = (b m - x) mod b m Diminished radix complement of x x c = b m - 1 - x The complement of a number in the range 0  x  b m -1 is in the same range The mod b m in the radix complement definition makes this true for x = 0; it has no effect for any other value of x Specifically, the radix complement of 0 is 0

18 Complement Representations of Negative Numbers For even b, radix complement system represents one more negative than positive value While diminished radix complement system has 2 zeros but represents same number of positive & negative values Radix ComplementDiminished Radix Complement Number Representation 0000 or b m -1 0<x<b m /2x x -b m /2  x<0 |x| c = b m - |x| |x| c = b m - 1 - |x| -b m /2<x<0

19 Base 2 Complement Representations In 1’s complement, 255 = 11111111 2 is often called -0 In 2’s complement, -128 = 10000000 2 is a legal value, but trying to negate it gives overflow 8 Bit 2’s Complement8 Bit 1’s Complement Number Representation 0 000 or 255 0<x<128x x -128  x<0 256 - |x| 255 - |x| -127  x<0

20 Digitwise Computation of the Diminished Radix Complement Using the geometric series formula, the b-1’s complement of x can be written If 0  x i  b-1, then 0  (b-1-x i )  b-1, so last formula is just an m-digit base b number with each digit obtained from the corresponding digit of x

21 Conversion Between Related Bases by Digit Grouping Let base b = c k ; for example b = c 2 Then base b number x 1 x 0 is base c number y 3 y 2 y 1 y 0, where x 1 base b = y 3 y 2 base c and x 0 base b = y 1 y 0 base c Examples: 102130 4 = 10 21 30 4 = 49C 16 49C 16 = 0100 1001 1100 2 102130 4 = 01 00 10 01 11 00 2 010010011100 2 = 010 010 011 100 2 = 2234 8

22 Addition and Subtraction addition bits are added and carry propagated leftwards 0000 0111 7 ten + 0000 0110 6 ten = 0000 1101 13 ten subtraction use negation and addition i.e. x - y = x + (-y) 0000 0111 7 ten - 1111 1010 - 6 ten (2’s complement) = (1) 0000 0001 1 ten

23 Addition and Subtraction Addition (and subtraction) of binary numbers is similar to ordinary addition. If the sum won’t fit in one bit, you need to carry one to the next place on the left. The rules are: 0 + 0 = 01 + 0 = 10 + 1 = 1 1 + 1 = 0 and a carry 1+ 1 + 1 (from a previous carry) = 1 and a carry

24 Examples: (the second illustrates a “ripple effect”) 10 10 11 0001 11 11 11 +00 00 11 1100 00 00 10 ------------------------------------------------------------------------------------- 10 11 10 1110 00 00 01 0 0 0 1 1 0 0 1 1 1 1 1 1 0 Long binary numbers get hard for humans to read: 1100 1010 1110 1100 1011 0101 1100 1000 1111 1010 0111 0110 0001 0010 0011 0100 0110 0101, etc… and it takes a long time to convert them into decimal ===> we need a more readable representation: Bases 8, 16

25 Carry and Overflow need to take account of restricted word size carry : when unsigned numbers are added, a carry occurs when a carry is propagated from the leftmost bit 1100 0000 192 ten + 0100 0000 64 ten = (1) 0000 0000 0 ten (256) overflow : when signed numbers are added, a overflow occurs when the carry into the sign bit differs from the carry out of it 0100 0000 64 ten + 0100 0000 64 ten 1000 0000 -128 ten

26 Multiplication algorithm operates on two unsigned n-bit numbers, a and b initialisation load a into multiplier register A load b into multiplier register B clear register P repeat n times 1. if rightmost bit of A is 1-bit, then add B to P 2. shift P and A right, with rightmost bit of P shifting into leftmost bit of A product is in P and A

27 Multiplication Example a =5, b = 3 P: 0000 A: 0101 B: 0011 Iteration 1 P: 0001  A: 1010 B: 0011 Iteration 2 P: 0000  A: 1101 B: 0011 Iteration 3 P: 0001  A: 1110 B: 0011 Iteration 4 P: 0000  A: 1111 B: 0011 product in PA : 0000 1111 answer = 15

28 Division algorithm operates on two unsigned n-bit numbers, a and b initialisation load a into divider register A, load b into divider register B clear register P repeat n times 1. shift P and A left, with leftmost bit of A shifting into rightmost bit of P 2. subtract B from P 3. if result of subtraction is negative, set rightmost bit of A to 0-bit, otherwise set to 1-bit and add B to P restoring its value quotient in A, remainder in P

29 Division example, a = 14, b = 3 P: 0000 A: 1110 B: 0011 Iteration 1 P: 0001  A: 1100 B: 0011 Iteration 2 P: 0000  A: 1001 B: 0011 Iteration 3 P: 0001  A: 0010 B: 0011 Iteration 4 P: 0010  A: 0100 B: 0011 quotient in A = 4 remainder in P = 2


Download ppt "Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi."

Similar presentations


Ads by Google