140 likes | 329 Views
ELEC1700 Computer Engineering 1 Week 2 Wednesday lecture Number systems and arithmetic Semester 1, 2013. Number systems and arithmetic. Signed numbers Arithmetic with signed numbers. Signed numbers in 2’s complement form. To represent a signed binary number in 2’s complement form:
E N D
ELEC1700Computer Engineering 1Week 2 Wednesday lectureNumber systems and arithmetic Semester 1, 2013
Number systems and arithmetic Signed numbers Arithmetic with signed numbers
Signed numbers in 2’s complement form To represent a signed binary number in 2’s complement form: Positive numbers: represented as “normal” binary numbers, i.e. same as positive sign-magnitude Negative numbers: represented as 2’s complement of corresponding positive number Example:Represent −2510 in 8-bit 2’s complement form +2510 = 000110012 2’s complement of 00011001 is 11100111 8-bits = one byte
Decimal value of signed numbers in 2’s complement form The negative number −2510 is written in 8-bit 2’s complement form as: Note that 11001112 ≠ 2510 This isn’t sign-magnitude form! −25 =11100111 Sign bit Magnitude bits An easy way to read a signed number that uses this notation is to assign the sign bit a column weight of −128 (for an 8-bit number). Then add the column weights for the 1’s.For an n-bit number, column weight of the sign bit is −2n-1 Example: show that 11000110 represents −58 as an 8-bit 2’s complement signed number: Column weights: −12864 32 16 8 4 2 1 1 1 0 0 0 1 1 0 −128+64 +4 +2 = −58
2’s complement: a graphical view • Add n: move the arrow n positions clockwise • Subtract n: move the arrow n positions anticlockwise • 4-bit 2’s complement range: −8 to +7 Sign bit has column weight −2n-1 for n-bit number. Here n=4, so weight is −8 Column weights: −84 2 1 1 0 1 1 −8+2 +1 = −510
Express +6810 as a binary number in 8-bit 2’s complement form Express −4110 as a binary number in 8-bit 2’s complement form
Negation Negation: converting a positive number to its negative equivalent (or vice versa) Example: +9 is the negation of −9 Let’s check that negation works for signed binary numbers in 2’s complement form… Start with 5-bit representation 01001 +9 2’s complement (negate) 10110 + 1 = 10111 −9 …and negate again 01000 + 1 =01001+9
Comparison of number systems Table shows 4-bit numbers (not all; just some) unsigned binary system has no negative numbers sign-magnitude and 1’s complement number systems have two representations for “0” DecimalUnsignedSign-magnitude1’s complement2’s complement +20010001000100010 +10001000100010001 +0000000000000 0000 −0N/A1000 1111 0000 −1N/A100111101111 −2N/A101011011110
Range of integers in various formats With n bits, can represent 2n combinations Unsigned numbers: Range = 0,1,2,…,2n-1 Example: n=8, range is 0,1,2,…,255 Sign-magnitude form: Range = −(2n-1-1) to +(2n-1-1) Example: n=8, range is −127 to +127 1’s complement form: Range = −(2n-1-1) to +(2n-1-1) Example: n=8, range is −127 to +127 2’s complement form: Range = −(2n-1) to +(2n-1-1) Example: n=8, range is −128 to +127
Number systems and arithmetic Signed numbers Arithmetic with signed numbers
Discard carry out of sign bit Arithmetic with signed binary numbers: addition • To add two signed numbers in 2’s complement form: • Perform the addition • Discard any final carry out of MSB (=sign bit) • Result is in signed 2’s complement form • Result will be correct provided range is not exceeded • Three examples: 00011110 = +30 00001111 = +15 00001110 = +14 11101111 = -17 11111111 = -1 11111000 = -8 00101101 = +45 11111101 = -3 1 11110111 = -9
Discard carry Overflow condition • Overflow occurs when result of addition is out of range • number of bits required to represent sum exceeds number of bits in the two numbers added • occurs if (+A) + (+B) = −C or (−A) + (−B) = +C Examples: 01000000 = +64 01000001 = +65 10000001 = −127 10000001 = −127 10000001 = −127 100000010 = +2 Answers are both incorrect as sign bit of result disagrees with sign of augend and addend
Arithmetic with signed binary numbers: subtraction • To subtract two signed numbers in 2’s complement form: • Take 2’s complement of subtrahend, then add the minuend • A − B = A + (−B) • Digital computers can use the same circuitry to add and subtract → saving in hardware • Remember: 2’s complement of subtrahend B is the negation of B • Discard any final carry out of MSB. The result is in signed form. minuend − subtrahend = difference
(+30) –(+15) (+14) –(-17) (-1) –(-8) Discard carry Discard carry Arithmetic with signed binary numbers: subtraction Examples: same numbers on slide 14, but subtract rather than add: 00011110 00001111 00001110 11101111 11111111 11111000 - - - Take 2’s complement of subtrahend and add: 00011110 = +30 11110001 = -15 00001110 = +14 00010001 = +17 11111111 = -1 00001000 = +8 1 00001111 = +15 00011111 = +31 1 00000111 = +7