Negative Numbers and SubtractionSigned magnitude representationSigned magnitude operationsOne’s complement representationWhy is it called “one’s complement?”One’s complement additionWhy does this work (roughly)?Two’s complementMore about two’s complementTwo’s complement additionAnother two’s complement exampleWhy does this work?Comparing the signed number systemsRanges of the signed number systemsConverting signed numbers to decimalExample solutionSigned Numbers are Sign-Extended to Infinity!Making a subtraction circuitA two’s complement subtraction circuitSmall differencesAn adder-subtractor circuitSigned overflowDetecting signed overflowSign extensionSubtraction summarySubtraction (lvk) 1Negative Numbers and Subtraction•The adders we designed can add only non-negative numbers–If we can represent negative numbers, then subtraction is “just” the ability to add two numbers (one of which may be negative).•We’ll look at three different ways of representing signed numbers.•How can we decide representation is better?–The best one should result in the simplest and fastest operations.–This is just like choosing a data structure in programming.•We’re mostly concerned with two particular operations:–Negating a signed number, or converting x into -x. –Adding two signed numbers, or computing x + y.–So, we will compare the representation on how fast (and how easily) these operations can be done on themSubtraction (lvk) 2Signed magnitude representation•Humans use a signed-magnitude system: we add + or - in front of a magnitude to indicate the sign.•We could do this in binary as well, by adding an extra sign bit to the front of our numbers. By convention:–A 0 sign bit represents a positive number.–A 1 sign bit represents a negative number.•Examples:11012= 1310(a 4-bit unsigned number)0 1101 = +1310(a positive number in 5-bit signed magnitude)1 1101 = -1310(a negative number in 5-bit signed magnitude)01002= 410(a 4-bit unsigned number)0 0100 = +410(a positive number in 5-bit signed magnitude)1 0100 = -410(a negative number in 5-bit signed magnitude)Subtraction (lvk) 3Signed magnitude operations•Negating a signed-magnitude number is trivial: just change the sign bit from 0 to 1, or vice versa.•Adding numbers is difficult, though. Signed magnitude is basically what people use, so think about the grade-school approach to addition. It’s based on comparing the signs of the augend and addend:–If they have the same sign, add the magnitudes and keep that sign.–If they have different signs, then subtract the smaller magnitude from the larger one. The sign of the number with the larger magnitude is the sign of the result.•This method of subtraction would lead to a rather complex circuit. + 3 7 9+ - 6 4 7 -2 6 85 13 176 4 7- 3 7 92 6 8 becauseSubtraction (lvk) 4One’s complement representation•A different approach, one’s complement, negates numbers by complementing each bit of the number.•We keep the sign bits: 0 for positive numbers, and 1 for negative. The sign bit is complemented along with the rest of the bits.•Examples:11012= 1310(a 4-bit unsigned number)0 1101 = +1310(a positive number in 5-bit one’s complement)1 0010 = -1310(a negative number in 5-bit one’s complement)01002= 410(a 4-bit unsigned number)0 0100 = +410(a positive number in 5-bit one’s complement)1 1011 = -410(a negative number in 5-bit one’s complement)Subtraction (lvk) 5Why is it called “one’s complement?”•Complementing a single bit is equivalent to subtracting it from 1.0’ = 1, and 1 - 0 = 1 1’ = 0, and 1 - 1 = 0•Similarly, complementing each bit of an n-bit number is equivalent to subtracting that number from 2n-1.•For example, we can negate the 5-bit number 01101.–Here n=5, and 2n-1 = 3110 = 111112.–Subtracting 01101 from 11111 yields 10010:1 1 1 1 1- 0 1 1 0 11 0 0 1 0Subtraction (lvk) 6One’s complement addition•To add one’s complement numbers:–First do unsigned addition on the numbers, including the sign bits.–Then take the carry out and add it to the sum.•Two examples:•This is simpler and more uniform than signed magnitude addition.0111 (+7)+ 1011 + (-4)1 00100010+ 10011 (+3)0011 (+3)+ 0010 + (+2)0 01010101+ 00101 (+5)Subtraction (lvk) 7Why does this work (roughly)?•When both numbers have a sign bit, a carry-out is generated:–(2n-1 – 1 - A) + (2n-1 – 1 - B)–(2n – 2 – (A + B))•The sum we want in this case is–(2n – 1 – (A + B))•Therefore adding one when a carry is generated corrects the “-2” into a “-1”.Subtraction (lvk) 8Two’s complement•Our final idea is two’s complement. To negate a number, complement each bit (just as for ones’ complement) and then add 1. •Examples:11012= 1310(a 4-bit unsigned number)0 1101 = +1310(a positive number in 5-bit two’s complement)1 0010 = -1310(a negative number in 5-bit ones’ complement)1 0011 = -1310(a negative number in 5-bit two’s complement)01002= 410(a 4-bit unsigned number)0 0100 = +410(a positive number in 5-bit two’s complement)1 1011 = -410(a negative number in 5-bit ones’ complement)1 1100 = -410(a negative number in 5-bit two’s complement)Subtraction (lvk) 9•Two other equivalent ways to negate two’s complement numbers:–You can subtract an n-bit two’s complement number from 2n.–You can complement all of the bits to the left of the rightmost 1.01101 = +1310(a positive number in two’s complement)10011 = -1310(a negative number in two’s complement)00100 = +410(a positive number in two’s complement) 11100 = -410(a negative number in two’s complement)•Often, people talk about “taking the two’s complement” of a number. This is a confusing phrase, but it usually means to negate some number that’s already in two’s complement format.More about two’s complement100000- 01101 (+1310)10011 (-1310)100000- 00100 (+410)11100 (-410)Subtraction (lvk) 10Two’s complement addition•Negating a two’s complement number takes a bit of work, but addition is much easier than with the other two systems.•To find A + B, you just have to:–Do unsigned addition on A and B, including their sign bits. –Ignore any carry out.•For example, to find 0111 + 1100, or (+7) + (-4): –First add 0111 + 1100 as unsigned numbers:–Discard the carry out (1).–The answer is 0011 (+3).0111+ 110010011Subtraction (lvk) 11Another two’s complement example•To further convince you that this works, let’s try
View Full Document