1 / 31

Freshman Computer Engineering/Science

Learn how to perform addition and subtraction in any numbering system, including decimal, using the same rules and techniques. Understand the concept of complements and how to convert numbers to and from complement form.

tuesday
Download Presentation

Freshman Computer Engineering/Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Freshman Computer Engineering/Science Computer Arithmetic

  2. 3 + 6 = 9  9/10 = 0 + 9/10  write 9 and carry 0 8 + 4 = 12  12/10 = 1 + 2/10  write 2 and carry 1 1+ 7 + 1 = 9  9/10 = 0 + 9/10  write 9 and carry 0 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: 7 4 3 1 8 6 + 9 2 9 • 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.

  3. 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

  4. Worksheet: Solution

  5. 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: 7 4 9 1 8 6 - 5 6 3 • 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. (96)  9 - 6 = 3  write 3 and borrow 0 from next column (4<8)  borrow 1  10+4-8=6  write 6 and borrow 1 from next col. (7-11) 7 - 1 - 1 = 5 write 5 and borrow 0 from next column

  6. 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:

  7. Worksheet: (Solution)

  8. 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 + 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

  9. 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

  10. 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 +(5A)16 = (5A)16’s cmpl -(5A)16 = (F5)15’s cmpl = (F6)16’s cmpl

  11. 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 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

  12. 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.

  13. 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 • (51A9)16- (7827)16 = (?)16

  14. 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 • (51A9)16- (7827)16 = (?)16

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

  16. +ve sign bit = 0 magnitude magnitude 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. (+38)10 = +(100110)2 = (00100110)SM:8 -ve sign bit = 1 (-38)10 = -(100110)2 = (10100110)SM:8 • The problem with the SM is that there are two values for zero: (0000000 = +0, and 10000000 = -0).

  17. 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 11111111 = -0).

  18. +1 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 • The two’s complement is widely used in computers to represent signed integers.

  19. Signed numbers: All methods using 4-bit representation Computer Arithmetic 19

  20. Worksheet: Fill up the table below using 8-bit representation 20

  21. Worksheet: (Solution) 21

  22. Homework: Fill up the table below using 8-bit representation 22

  23. 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

  24. Negative Numbers in Computers short int x = 45; x = 45 = (00101101)2 = (00101101)2’s Compiler short int y = -29; y = -29 = - (00011101)2 = (11100011)2’s short int z = -65; z = -65 = -(01000001)2 = (10111111)2’s x alu short int w = x + y; y w = 45 + (-29) = 00101101 + 11100011 = 1 00010000 = 16 y x add short int w = x + z; y y 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

  25. 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 = (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

  26. 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.

  27. Fractional Numbers: Floating Point Representation • Common methods used to represent floating point numbers Distance Engineering Notation Scientific Notation Normalized as x.xxx Normalized as 0.xxx E Notation Distance 1 127 m 127 m 1. 270x102m 0.127x103m 0.1270E+3 m Distance 2 1.764 Km 1.764x103m 1. 764x103m 0.1764x104m 0.1764E+4 m Distance 3 79,3 Km 79.3x103m 7.93x104m 0.793x105m 0.7930E+5 m Distance 4 6.45 m 6.45 m 6.45x100m 0.645x101m 0.6450E+1 m Distance 5 8.2 cm 8.2x10-2m 8.2x10-2m 0.82x10-1m 0.8200E-1 m Distance 6 6.5 mm 6.5x10-3m 6.5x10-3m 0.65x10-2m 0.6500E-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.9999E+9 = 999000000 • Underflow: (integer normalized as 0.xxx and a 1-digit signed exponent) • Number <0.000000000001

  28. Integer Fraction ± 0 0 1 0 0 0 1 0 1 1 1 1 0 0 0 0 -511.953125 -511.984375 511.984375 511.953125 -511.96875 511.96875 -0.046875 -0.015625 -511.9375 0.015625 0.046875 511.9375 -0.03125 0.03125 -0.0625 0.0625 0.0 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. -45.375 = -(101101.011)2 = (1000101101.011000)2 = (1000101101011000)fixed (0001101101101000)fixed = (0001101101.101000)2 = +(1101101.101)2 = +109.625 Overflow limit = (0111111111111111)fixed = +(111111111.111111)2 = +511.984 Underflow limit = (0000000000000001)fixed = +(0.000001)2 = +2-6 = +0.015625

  29. Binary Floating Point Numbers • Consider a 16-bit floating-point binary system where the 11 MSBs represent the signed magnitude mantissa normalized as 0.1xx, 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 0 1 1 1 0 0 1 0 0 -45.375 = -(101101.011)2 = -(0.10110101100)2x26 = (11011010110000110)float (0101100000011000)float = (0.1011000000)2 x 2-8 = +(1011)2 x 2-12 = +11 x 2-12 Overflow limit = (0111111111101111)float = +(0.11111111111)2 x 215 +32768 Underflow limit = (0100000000011111)float = +(0.10000000000)2x2-15 = 2-1 x 2-15= 2-16 E = +15 E = -15 E = -15 E = -14 E = +14 E = +15 -2+15+2x2+4 -2-16-2x2-26 2-16+2x2-26 -2+15+2+4 2+15-2x2+4 2+15-3x2+4 2-15+2-25 2+13+2+3 -2-16-2-26 2-16+2-26 0.0 ?? 2-14-2-25 2+15-2+4 2+14-2+3 2-16-2-26 2+14 -2-16 2+13 2-16 2-15 2-26 2+4 2+3 2-25

  30. Examples: Do the following conversions

  31. 0 0 1 0 0 0 1 0 0 1 1 0 0 0 1 0 1 1 1 1 1 0 0 0 1 0 1 1 1 0 1 0 ±(S) 8-bits 23-bits Excess-127 biased Exponent (E) Signed-Magnitude mantissa (M) normalized as 1.xxxxxx with the integer (1) implied (N)10 =± 1.M x 2 (E -127) 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 45.375 = (101101.011)2 = (1.0110101100)2x25  S = 0(positive number)  E = 127 + 5 = 132 = 10000100  M = 01101011000000000000000  45.375 =(01000010001101011000000000000000)float • (11000011001001100010000000000000)float = X •  S = 1(negative number) •  E = 10000110 = 134 • M = 01001100010000000000000 • X =-1.0100110001000x 2134-127= -1.0100110001 x 27 = -10100110.001 = -166.125

More Related