Freshman Computer EngineeringScience Computer Arithmetic 1 Addition in

  • Slides: 31
Download presentation
Freshman Computer Engineering/Science Computer Arithmetic 1

Freshman Computer Engineering/Science Computer Arithmetic 1

Addition in any numbering system • Addition in any numbering system can be performed

Addition in any numbering system • Addition in any numbering system can be performed by following the same rules used for decimal addition, where 10 is replaced by the base of the system (R). • Decimal Addition: 3 + 6 = 9 9/10 = 0 + 9/10 write 9 and carry 0 7 4 3 8 + 4 = 12 12/10 = 1 + 2/10 write 2 and carry 1 1 8 6+ 9 2 9 1+ 7 + 1 = 9 9/10 = 0 + 9/10 write 9 and carry 0 • Rules for addition in the decimal system: – Begin the addition by adding the 2 least significant digits first. – Perform the integer division of the sum with 10. Write down the remainder of the division and carry out the result to the next column. – Repeat the addition for the next columns by adding the two digits and the carry from the previous column. 2

Worksheet: Addition in any numbering system • Rules for addition in a system with

Worksheet: Addition in any numbering system • Rules for addition in a system with base R: – Begin the addition by adding the 2 least significant digits first. – Perform the integer division of the sum with R. Write down the remainder of the division and carry out the result to the next column. – Repeat the addition for the next columns by adding the two digits and the carry from the previous column. Examples: Perform the following additions (173)8 (01101011)2 (1243)5 (4543)7 (265)8 + (00111010)2 + ( 234)5 + (2632)7+ (1 A 79)14 (1 A 79)16 (1 A 79)18 (2454)6 (C 927)14 + (C 927)16 + (C 927)18 + (1532)6 (2343)6+ 3

Worksheet: Solution (173)8 (01101011)2 (1243)5 (4543)7 (265)8 + (00111010)2 + ( 234)5 + (2632)7+

Worksheet: Solution (173)8 (01101011)2 (1243)5 (4543)7 (265)8 + (00111010)2 + ( 234)5 + (2632)7+ 460 10100101 (1 A 79)14 2032 10505 (1 A 79)16 (1 A 79)18 (2454)6 (C 927)14 + (C 927)16 + (C 927)18 + (1532)6 105 A 2 E 3 A 0 E 19 G (2343)6+ 11213 4

Subtraction in any numbering system • Subtraction in any numbering system can be performed

Subtraction in any numbering system • Subtraction in any numbering system can be performed by following the same rules used for decimal addition, where 10 is replaced by the base of the system (R). • Decimal Subtraction: (9 6) 9 - 6 = 3 write 3 and borrow 0 from next column 7 4 9 1 8 65 6 3 (4<8) borrow 1 10+4 -8=6 write 6 and borrow 1 from next col. (7 -1 1) 7 - 1 = 5 write 5 and borrow 0 from next column • Rules for subtraction in the decimal system: – Begin the subtraction from 2 least significant digits first. – If the minuend is greater than the subtrahend then perform the subtraction. – If the minuend is less than the subtrahend then borrow 1 from the next column. Write down the result of (minuend + 10 - subtrahend). The one borrowed must be subtracted from the minuend of the next column. – Repeat the subtraction for the next columns. 5

Worksheet: Subtraction in any numbering system • Rules for subtraction in a system with

Worksheet: Subtraction in any numbering system • Rules for subtraction in a system with base R: The rules for subtraction in a system with base R are the same as in decimal, except that a borrow into a given column adds R units to the minuend digit. Examples: Perform the following subtractions: (273)8 (011011)2 (4143)5 (4523)7 (165)8 - (001110)2 - (3234)5 - (2635)7 - (C 374)14 (C 374)16 (476)10 (285)10 (4927)14 - (4927)16 - (285)10 - (476)10 - 6

Worksheet: (Solution) (273)8 (011011)2 (4143)5 (4523)7 (165)8 - (001110)2 - (3234)5 - (2635)7 -

Worksheet: (Solution) (273)8 (011011)2 (4143)5 (4523)7 (165)8 - (001110)2 - (3234)5 - (2635)7 - 106 001101 0404 1555 (C 374)14 (C 374)16 (476)10 (285)10 (4927)14 - (4927)16 - (285)10 - (476)10 - 784 B 7 A 4 D 191 809 (? ? ) 7

The 10’s complement • In the previous examples, the subtraction {(285)10 - (476)10 }

The 10’s complement • In the previous examples, the subtraction {(285)10 - (476)10 } give a result equal to 809. – The correct result is -191. • If we examine carefully the subtraction we can observe that the subtraction of the last digit was carried out using a borrow, i. e. the result 809 is obtained after we borrow 1000. • If we add 809 and 191 then we get 1000, which is what is borrowed in order to complete the subtraction. – The addition of each digit of the numbers 809 and 191 always is equal to 10 • [9 + 1 = 10], [0 + 9 + 1 =10], [8 + 1 = 10] • The number 809 is said to be the 10’s complement of the number 191. This shows that: – If the result of a subtraction is a negative number, then this number is represented in the complement’s form. – In the 10, s complement system: • Positive numbers begin with a digit from 0 to 4 • Negative numbers begin with a digit from 5 to 9 8

The R’s complement • In any numbering system with base R, we can represent

The R’s complement • In any numbering system with base R, we can represent negative numbers with the R’s complement – – In the decimal system we use the 10’s complement In the binary system we use the 2’s complement In the octal system we use the 8’s complement In the hexadecimal system we use the 16’s complement • In an R’s complement system with N digits, the first half possible numbers are positive, and the rest negative. – In the 10’s complement with 2 digits, • 00 to 49 represent the positive numbers from 00 to 49 • 50 to 99 represent the negative numbers from -50 to -01 – In the 2’s complement with 4 digits, • 0000 to 0111 represent the positive numbers from 0 to 7 • 1000 to 1111 represent the negative numbers from -8 to -1 • If a number in the R’s complement is positive, then this number begins with a digit less that R/2 and has the same value as the same number in the system with base R – If the number is negative, then it begins with a digit greater than R/2 9

Conversion to the R’s complement • If a number is positive, then this number

Conversion to the R’s complement • If a number is positive, then this number remains the same in the R’s complement • If a number is negative, then to find it’s R’s complement – Find first the R-1 complement, and then add 1 to find the R’s complement • Examples: +(346)10 = (346)10’s cmpl -(346)10 = (653)9’s cmpl = (654)10’s cmpl +(011011)2 = (011011)2’s cmpl -(011011)2 = (100100)1’s cmpl = (100101)2’s cmpl +(5 A)16 = (5 A)16’s cmpl -(5 A)16 = (F 5)15’s cmpl = (F 6)16’s cmpl 10

Conversion from the R’s complement • To convert a number from the R’s complement

Conversion from the R’s complement • To convert a number from the R’s complement to R: • If the number begin with a digit less than R/2, then the number is positive and remains the same as in the R’s complement • If the number begin with a digit greater or equal to R/2, then the number is negative – Subtract 1 to find the (R-1) complement and then find the number in R – OR find first the (R-1)’ complement, and then add 1 to find the number in R • Examples: (436)10’cmpl = +(436)10 {the number is +ve since it begins with 4} (734)10’s cmpl = (733)9’s cmpl = -(266)10 {the number is -ve since it begins with 7} OR -(734)10’s cmpl = (265)x’s cmpl = -(266)10 (011011)2’s cmpl = +(011011)2 {the number is +ve since it begins with 0} (101011)2’s cmpl = (101010)1’s cmpl = -(010101)2 {-ve since it begins with 1} OR (101011)2’s cmpl = (010100)x’s cmpl = -(010101)2 11

Subtraction using the complement’s method • In the previous examples, the subtraction {(285)10 -

Subtraction using the complement’s method • In the previous examples, the subtraction {(285)10 - (476)10 } give a result equal to 809. The correct result is -191. • If we examine carefully the subtraction we can observe that the subtraction of the last digit was carried out using a borrow, i. e. the result 809 is obtained after we borrow 1000. • If we add 809 and 191 then we get 1000, which is what is borrowed in order to complete the subtraction. • The number 809 is said to be the 10’s complement of the number 191. This shows that: – Negative numbers can be represented in the complement’s form. – Subtraction in the decimal system, can be carried out by adding to the minuend the 10’ s complement of the subtrahend. i. e. (285)10 - (476)10 = (285)10 + (-476)10 = (285)10 + [ (523)9’s ++ 1] = (285)10’s + (524)10’s = (809)10’s – Subtraction in a system with base R, can be carried out by adding to the minuend the R’ s complement of the subtrahend. 12

Worksheet: Subtraction using the complement’s method Perform the following subtractions using the R’s complement

Worksheet: Subtraction using the complement’s method Perform the following subtractions using the R’s complement method: • (476)10 - (285)10 = (? )10 • (285)10 - (476)10 = (? )10 • (173)8 - (265)8 = (? )8 • (51 A 9)16 - (7827)16 = (? )16 13

Worksheet: (Solution) • (476)10 - (285)10 = (? )10 = (476)10 +(-285)10 = (476)10

Worksheet: (Solution) • (476)10 - (285)10 = (? )10 = (476)10 +(-285)10 = (476)10 + (715)10’s = 1 191 = (191)10’s • (285)10 - (476)10 = (? )10 = (285)10 + (-476)10 = (285)10 + (524)10’s = 809 = (809)10’s (Note: (809)10’s is a negative number. (809)10’s = (-191)10 • (173)8 - (265)8 = (? )8 = (173)8 + (-265)8 = (173)8 + (513)8’s = 706 = (706)8’s (Note: (706)8’s is a negative number. (706)8’s = (-072)8 • (51 A 9)16 - (7827)16 = (? )16 14

Homework: Do the necessary operations to fill up the table below: 15

Homework: Do the necessary operations to fill up the table below: 15

Negative Number Representation: Signed Magnitude • In the Signed Magnitude representation the most significant

Negative Number Representation: Signed Magnitude • In the Signed Magnitude representation the most significant bit is used as the sign of the number. • The sign bit is set to zero for positive numbers and to one for negative numbers. +ve sign bit = 0 (+38)10 = +(100110)2 = (00100110)SM: 8 magnitude -ve sign bit = 1 (-38)10 = -(100110)2 = (10100110)SM: 8 magnitude • The problem with the SM is that there are two values for zero: (0000000 = +0, and 10000000 = -0). 16

Negative Number Representation: One’s Complement • The One’s Complement representation can be derived from

Negative Number Representation: One’s Complement • The One’s Complement representation can be derived from the Signed Magnitude representation. • If the number is positive then the one’s complement is the same as the SM. • If the number is negative then the one’s complement is obtained by inverting all magnitude bits of the SM while the sign bit is unchanged. sign bit (+38)10 = (00100110)SM = (00100110)1’s magnitude sign bit (-38)10 = (10100110)SM = (11011001)1’s magnitude • The problem with the One’s Complement is that there are two values for zero: (0000000 = +0, and 1111 = -0). 17

Negative Number Representation: Two’s Complement • If the number is positive then the two’s

Negative Number Representation: Two’s Complement • If the number is positive then the two’s complement is the same as the SM. • If the number is negative then the two’s complement is obtained by adding 1 to the magnitude bits of the one’s complement while the sign bit is unchanged. sign bit (+38)10 = (00100110)1’s = (00100110)2’s magnitude sign bit (-38)10 = (11011001)1’s = (11011010)2’s magnitude +1 • The two’s complement is widely used in computers to represent signed integers. 18

Signed numbers: All methods using 4 -bit representation Binary Dec. S. M. Dec. 1’s

Signed numbers: All methods using 4 -bit representation Binary Dec. S. M. Dec. 1’s Cmpl Dec. 2’s Cmpl Dec 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1 Computer Arithmetic 19

Worksheet: Fill up the table below using 8 -bit representation Decimal Binary Hex Sign

Worksheet: Fill up the table below using 8 -bit representation Decimal Binary Hex Sign Magnitude SM One’s Complement 15’s Two’s Complement 16’s +41 -41 10101100 01011000 -1 +128 -129 20

Worksheet: (Solution) Decimal Binary Hex Sign Magnitude SM One’s Complement 15’s Two’s Complement 16’s

Worksheet: (Solution) Decimal Binary Hex Sign Magnitude SM One’s Complement 15’s Two’s Complement 16’s +41 +29 +101001 29 00101001 -41 -29 +101001 A 9 10101001 D 6 11010110 D 7 11010111 -44 -2 C -101100 5 C 10101100 D 3 11010011 D 4 11010100 -83 -53 -1010011 D 3 11010011 AC 10101100 AD 10101101 -84 -54 -1010100 D 4 11010100 AB 10101011 AC 10101100 +88 +58 +1011000 58 01011000 -1 -1 -1 81 10000001 FE 11111110 FF 1111 +128 +80 +10000000 Overflow -128 -80 -10000000 Overflow -129 -81 -10000001 Overflow 80 10000000 Overflow 21

Homework: Fill up the table below using 8 -bit representation Decimal Binary Hex Sign

Homework: Fill up the table below using 8 -bit representation Decimal Binary Hex Sign Magnitude SM One’s Complement 15’s Two’s Complement 16’s +57 -57 11001010 01001010 11001011 01001110 9 A 01001110 11001010 AE 22

Negative Numbers in Computers • Negative numbers in computers are represented in the two’s

Negative Numbers in Computers • Negative numbers in computers are represented in the two’s complement form • Subtraction in computers is carried out by adding to the positive number the two’s complement of the negative number • In most languages such as Pascal and C an integer variable is represented in a 16 bit or a 32 -bit two’s complement representation Say what the compiler does Say what the HW (alu) does if + and if – Say what the runtime aka printf does • • • 23

Negative Numbers in Computers short int x = 45; x = 45 = (00101101)2’s

Negative Numbers in Computers short int x = 45; x = 45 = (00101101)2’s Compiler short int y = -29; short int z = -65; short int w = x + y; short int w = x + z; y = -29 = - (00011101)2 = (11100011)2’s z = -65 = -(01000001)2 = (10111111)2’s x y alu y add y w = 45 + (-29) = 00101101 + 11100011 = 1 00 y w = 45 + (-65) =00101101 + 10111111 = 11101100 = -20 short int w = x - y; w = 45 – (-29) = 00101101 + (00011100)+1 = 01001010 = 74 short int w = z + y; w = (-65) + (-29) = 10111111+11100011 = 1 10100010 = -94 short int w = z - y; w = (-65) - (-29) = 10111111+(00011100)+1= 11011100 = -36 24

Negative Numbers in Computers • Negative numbers in computers are represented in the two’s

Negative Numbers in Computers • Negative numbers in computers are represented in the two’s complement form • Subtraction in computers is carried out by adding to the positive number the two’s complement of the negative number • In most languages such as Pascal and C an integer variable is represented in a 16 bit or a 32 -bit two’s complement representation short int x = 45; x = 45 = (00101101)2’s short int y = -29; y = -29 = - (00011101)2 = (11100011)2’s short int z = -65; z = -65 = -(01000001)2 = (10111111)2’s short int w = x + y; w = 45 + (-29) = 00101101 + 11100011 = 1 00010000 = 16 short int w = x + z; w = 45 + (-65) =00101101 + 10111111 = 11101100 = -20 short int w = x - y; w = 45 – (-29) = 00101101 + (00011100)+1 = 01001010 = 74 short int w = z + y; w = (-65) + (-29) = 10111111+11100011 = 1 10100010 = -94 short int w = z - y; w = (-65) - (-29) = 10111111+(00011100)+1= 11011100 = -36 25

Fractional Numbers: Fixed Point Representation • Consider a 6 -digit decimal system used to

Fractional Numbers: Fixed Point Representation • Consider a 6 -digit decimal system used to represent a distance in meters. Assume that 4 digits are used to represent the integer part and 2 digits are used to represent the fractional part of the distance. Distance to the library 0127. 00 m Distance to the hostel 1764. 00 m Distance to the airport Overflow, distance is greater than 9999. 99 m Width of this classroom 0006. 45 m Width of the door 0000. 82 m Diameter of your pen Underflow, diameter is less than 0000. 01 m • Overflow: – A number is greater than the maximum number that can be represented in a numbering system. • Underflow: – A number is less than the minimum positive number, other than zero, that can be represented in a numbering system. 26

Fractional Numbers: Floating Point Representation • Common methods used to represent floating point numbers

Fractional Numbers: Floating Point Representation • Common methods used to represent floating point numbers Engineering Scientific Normalized Distance Notation as x. xxx as 0. xxx E Notation Distance 1 127 m 1. 270 x 102 m 0. 127 x 103 m 0. 1270 E+3 m Distance 2 1. 764 Km 1. 764 x 103 m 0. 1764 x 104 m 0. 1764 E+4 m Distance 3 79, 3 Km 79. 3 x 103 m 7. 93 x 104 m 0. 793 x 105 m 0. 7930 E+5 m Distance 4 6. 45 m 6. 45 x 100 m 0. 645 x 101 m 0. 6450 E+1 m Distance 5 8. 2 cm 8. 2 x 10 -2 m 0. 82 x 10 -1 m 0. 8200 E-1 m Distance 6 6. 5 mm 6. 5 x 10 -3 m 0. 65 x 10 -2 m 0. 6500 E-2 m • Consider a 6 -digit decimal system with 4 digits representing the mantissa and 2 digits the signed exponent: – Overflow: (integer normalized as 0. xxx and a 1 -digit signed exponent) • Number > 0. 9999 E+9 = 999000000 – Underflow: (integer normalized as 0. xxx and a 1 -digit signed exponent) • Number <0. 0000001 27

Binary Fixed Point Numbers • Consider a 16 -bit fixed-point binary system where the

Binary Fixed Point Numbers • Consider a 16 -bit fixed-point binary system where the 10 MSBs represent the signed magnitude integer, while the 6 least significant bits represent the fractional part of the number. ± Integer Fraction 0 0 1 0 1 1 0 0 -45. 375 = -(101101. 011)2 = (1000101101. 011000)2 = (10001011000)fixed (0001101101101000)fixed = (0001101101. 101000)2 = +(1101101. 101)2 = +109. 625 Overflow limit = (011111111)fixed = +(111111)2 = +511. 984 511. 953125 511. 96875 511. 984375 511. 9375 0. 0625 0. 046875 0. 015625 0. 03125 0. 0 -0. 015625 -0. 03125 -0. 0625 -0. 046875 -511. 9375 -511. 96875 -511. 953125 -511. 984375 Underflow limit = (000000001)fixed = +(0. 000001)2 = +2 -6 = +0. 015625 28

Binary Floating Point Numbers • Consider a 16 -bit floating-point binary system where the

Binary Floating Point Numbers • Consider a 16 -bit floating-point binary system where the 11 MSBs represent the signed magnitude mantissa normalized as 0. 1 xx, while the 5 least significant bits represent the signed magnitude exponent of the number. SM Normalized Mantissa SM Exponent ± 0 1 1 0 0 0 1 1 1 0 0 -45. 375 = -(101101. 011)2 = -(0. 101100)2 x 26 = (110110000110)float (0101100000011000)float = (0. 1011000000)2 x 2 -8 = +(1011)2 x 2 -12 = +11 x 2 -12 Overflow limit = (01111101111)float = +(0. 111111)2 x 215 +32768 Underflow limit = (010000011111)float = +(0. 100000)2 x 2 -15 = 2 -1 x 2 -15= 2 -16 2 -25 2+3 2+15 -2+4 2+15 -2 x 2+4 2+15 -3 x 2+4 E = +15 2+14 -2+3 2+13+2+3 E = +14 2+13 E = -14 2 -14 -2 -25 E = -15 2 -16 -2 -26 2 -15+2 -25 E = -15 -2 -16 -2 x 2 -26 -2 -16 -2 -26 -2 -16 0. 0 ? ? 2 -16+2 -26 2 -16+2 x 2 -26 -2+15+2 x 2+4 -2+15+2+4 E = +15 2+4 29

Examples: Do the following conversions Decimal Binary Fixed Point SM 16: 6 Floating Point

Examples: Do the following conversions Decimal Binary Fixed Point SM 16: 6 Floating Point SM 16: 5 +13. 125 -51. 75 1011110110100101 0000110100100011 110000110100011000011100 -5/64 30

IEEE Single Precision Floating Point Numbers • • A standard specified by IEEE to

IEEE Single Precision Floating Point Numbers • • A standard specified by IEEE to represent single precision (32 -bit) floating-point binary numbers. Used by most computer floating point hardware and software 0 0 1 1 0 0 0 1 0 1 1 1 0 1 0 8 -bits 23 -bits ±(S) Excess-127 biased Signed-Magnitude mantissa (M) normalized as Exponent (E) 1. xxxxxx with the integer (1) implied (N)10 =± 1. M x 2 (E -127) 45. 375 = (101101. 011)2 = (1. 0110101100)2 x 25 S = 0 (positive number) E = 127 + 5 = 132 = 10000100 M = 0110101100000000 45. 375 =(01000110101100000000)float (11000011001001100000000)float = X S = 1 (negative number) E = 10000110 = 134 M = 01001100000000 X = -1. 010011000 x 2134 -127 = -1. 0100110001 x 27 = -10100110. 001 = -166. 125 31