2Binary Addition by Hand You can add two binary numbers one column at a time starting from theright, just as you add two decimal numbersBut remember that it’s binary. For example, = 10 and you have to carry!Carry inAugendAddendSumThe initial carryin is implicitly 0most significantbit, or MSBleast significantbit, or LSB
3Adding Two BitsA hardware adder by copying the human addition algorithmHalf adder: Adds two bits and produces a two-bit result: a sum (the rightbit) and a carry out (the left bit)Here are truth tables, equations, circuit and block symbol0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10C = XYS = X’ Y + X Y’= X YBe careful! Now we’re using + for both arithmetic addition and the logical OR operation.
4Adding Three BitsBut what we really need to do is add three bits: the augend and addend,and the carry in from the right.= 00= 01= 01= 10= 01= 10= 10= 11
5Full AdderFull adder: Three bits of input, two-bit output consisting of a sum and acarry outUsing Boolean algebra, we get the equations shown hereXOR operations simplify the equations a bitWe used algebra because you can’t easily derive XORs from K-mapsS = m(1,2,4,7)= X’ Y’ Cin + X’ Y Cin’ + X Y’ Cin’ + X Y Cin= X’ (Y’ Cin + Y Cin’) + X (Y’ Cin’ + Y Cin)= X’ (Y Cin) + X (Y Cin)’= X Y CinCout = m(3,5,6,7)= X’ Y Cin + X Y’ Cin + X Y Cin’ + X Y Cin= (X’ Y + X Y’) Cin + XY(Cin’ + Cin)= (X Y) Cin + XY
6Full Adder CircuitThese things are called half adders and full adders because you can builda full adder by putting together two half adders!S = X Y CinCout = (X Y) Cin + XY
7Iterative Functions and circuits Multi-bit logical functions (AND, OR, etc.) have functional independence between bitse.g. for a multi-bit AND we haveFi= Ai . Bi (Bitwise and)Arithmetic functions DO have dependence bit to bit (carry or borrow)The functions are also the same at each bit positionThese functions are known as ITERATIVE
94-bit Ripple Carry Adder Four full adders together make a 4-bit adderThere are nine total inputs:Two 4-bit numbers, A3 A2 A1 A0 and B3 B2 B1 B0An initial carry in, CIThe five outputs are:A 4-bit sum, S3 S2 S1 S0A carry out, COImagine designing a nine-input adder without this hierarchical structure—you’d have a 512-row truth table with five outputs!
10An example of 4-bit addition Let’s try our initial example: A=1011 (eleven), B=1110 (fourteen)1. Fill in all the inputs, including CI=013. Use C1 to find C2 and S1 ( = 10)2. The circuit produces C1 and S0 ( = 01)114. Use C2 to compute C3 and S2 ( = 10)1 15. Use C3 to compute CO and S3 ( = 11)The final answer is (twenty-five)
11OverflowIn this case, note that the answer (11001) is five bits long, while the inputswere each only four bits (1011 and 1110). This is called overflowAlthough the answer is correct, we cannot use that answer in anysubsequent computations with this 4-bit adderFor unsigned addition, overflow occurs when the carry out is 1
12Hierarchical Adder Design When you add two 4-bit numbers the carry in is always 0, so why does the4-bit adder have a CI input?One reason is so we can put 4-bit adders together to make even largeradders! This is just like how we put four full adders together to makethe 4-bit adder in the first placeHere is an 8-bit adder, for example.CI is also useful for subtraction!
13Gate DelaysEvery gate takes some small fraction of a second between the time inputsare presented and the time the correct answer appears on the outputs.This little fraction of a second is called a gate delayThere are actually detailed ways of calculating gate delays that can getquite complicated, but for this class, let’s just assume that there’s somesmall constant delay that’s the same for all gatesWe can use a timing diagram to show gate delays graphicallyxx’1gate delays
14Delays in the Ripple Carry Adder The diagram below shows a 4-bit adder completely drawn outThis is called a ripple carry adder, because the inputs A0, B0 and CI “ripple”leftwards until CO and S3 are producedRipple carry adders are slow!Our example addition with 4-bit inputs required 5 “steps”There is a very long path from A0, B0 and CI to CO and S3For an n-bit ripple carry adder, the longest path has 2n+1 gatesImagine a 64-bit adder. The longest path would have 129 gates!123456789
15A faster way to compute carry outs gipiInstead of waiting for the carry out from allthe previous stages, we could compute itdirectly with a two-level circuit, thusminimizing the delayFirst we define two functionsThe “generate” function gi produces 1when there must be a carry out from position i (i.e., when Ai and Bi are both 1).gi = AiBiThe “propagate” function pi is true when, ifthere is an incoming carry, it is propagated(i.e, when Ai=1 or Bi=1, but not both).pi = Ai BiThen we can rewrite the carry out function:ci+1 = gi + pici
16Algebraic Carry OutLet’s look at the carry out equations for specific bits, using the general equation from the previous page ci+1 = gi + pici:These expressions are all sums of products, so we can use them to make a circuit with only a two-level delay.c1 = g0 + p0c0c2 = g1 + p1c1= g1 + p1(g0 + p0c0)= g1 + p1g0 + p1p0c0c3 = g2 + p2c2= g2 + p2(g1 + p1g0 + p1p0c0)= g2 + p2g1 + p2p1g0 + p2p1p0c0c4 = g3 + p3c3= g3 + p3(g2 + p2g1 + p2p1g0 + p2p1p0c0)= g3 + p3g2 + p3p2g1 + p3p2p1g0 + p3p2p1p0c0Ready to see the circuit?
18Carry Lookahead Adders This is called a carry lookahead adderBy adding more hardware, we reduced the number of levels in the circuit and sped things upWe can “cascade” carry lookahead adders, just like ripple carry addersHow much faster is this?For a 4-bit adder, not much. CLA: 4 gates, RCA: 9 gatesBut if we do the cascading properly, for a 16-bit carry lookahead adder,8 gates vs. 33Newer CPUs these days use 64-bit adders. That’s 12 vs. 129 gates!The delay of a carry lookahead adder grows logarithmically with the sizeof the adder, while a ripple carry adder’s delay grows linearlyTrade-off between complexity and performance. Ripple carry adders aresimpler, but slower. Carry lookahead adders are faster but more complex
19Ripple Carry Between Groups 74x283 : 4-bit CLA adder74x283’s C4 output has 4gate delays16-bit adder with 16 gatedelaysRCA: 16x2 + 1 = 33
20Multiplication Multiplication can’t be that hard! It’s just repeated additionIf we have adders, we can do multiplication alsoRemember that the AND operation is equivalent to multiplication on twobits:
21Binary multiplication example Since we always multiply by either 0 or 1, the partial products are alwayseither 0000 or the multiplicand (1101 in this example)There are four partial products which are added to form the resultWe can add them in pairs, using three addersEven though the product has up to 8 bits, we can use 4-bit adders ifwe “stagger” them leftwards, like the partial products themselvesMultiplicandx MultiplierPartial productsProduct
22A 2x2 Binary Multiplier The AND gates produce the partial products For a 2-bit by 2-bit multiplier,we can just use two half addersto sum the partial products. Ingeneral, though, we’ll need fulladdersHere C3-C0 are the product, notcarries!
25More on Multipliers Multipliers are very complex circuits In general, when multiplying an m-bit number by an n-bit number:There are n partial products, one for each bit of the multiplierThis requires n-1 adders, each of which can add m bits (the size of the multiplicand)The circuit for 32-bit or 64-bit multiplication would be huge!
26Multiplication: a special case In decimal, an easy way to multiply by 10 is to shift all the digits to theleft, and tack a 0 to the right end128 x 10 = 1280We can do the same thing in binary. Shifting left is equivalent tomultiplying by 2:11 x 10 = 110 (in decimal, 3 x 2 = 6)Shifting left twice is equivalent to multiplying by 4:11 x 100 = 1100 (in decimal, 3 x 4 = 12)As an aside, shifting to the right is equivalent to dividing by 2.110 ÷ 10 = 11 (in decimal, 6 ÷ 2 = 3)
27Addition and Multiplication Summary Adder and multiplier circuits mimic human algorithms for addition andmultiplicationAdders and multipliers are built hierarchicallyWe start with half adders or full adders and work our way upBuilding these functions from scratch with truth tables and K-mapswould be pretty difficultThe arithmetic circuits impose a limit on the number of bits that can beadded. Exceeding this limit results in overflowThere is a tradeoff between simple but slow circuits (ripple carry adders)and complex but fast circuits (carry lookahead adders)Multiplication and division by powers of 2 can be handled with simpleshifting
28Signed NumbersThe arithmetic we did so far was limited to unsigned (positive) integersHow about negative numbers and subtraction?We’ll look at three different ways of representing signed numbersHow can we decide which representation is better?The best one should result in the simplest and fastest operationsWe’re mostly concerned with two particular operations:Negating a signed number, or converting x into -xAdding two signed numbers, or computing x + y
29Signed Magnitude Representation Humans use a signed-magnitude system: we add + or - in front of amagnitude to indicate the signWe could do this in binary as well, by adding an extra sign bit to thefront of our numbers. By convention:A 0 sign bit represents a positive numberA 1 sign bit represents a negative numberExamples:11012 = 1310 (a 4-bit unsigned number)= (a positive number in 5-bit signed magnitude)= (a negative number in 5-bit signed magnitude)01002 = 410 (a 4-bit unsigned number)= +410 (a positive number in 5-bit signed magnitude)= -410 (a negative number in 5-bit signed magnitude)
30One’s Complement Representation A different approach, one’s complement, negates numbers by complementingeach bit of the numberWe keep the sign bits: 0 for positive numbers, and 1 for negative. The signbit is complemented along with the rest of the bitsExamples:11012 = 1310 (a 4-bit unsigned number)= (a positive number in 5-bit one’s complement)= (a negative number in 5-bit one’s complement)01002 = 410 (a 4-bit unsigned number)= +410 (a positive number in 5-bit one’s complement)= -410 (a negative number in 5-bit one’s complement)
31Why is it called “one’s complement?” Complementing a single bit is equivalent to subtracting it from 10’ = 1, and = 1 1’ = 0, and = 0Similarly, complementing each bit of an n-bit number is equivalent tosubtracting that number from 2n-1 ( )For example, we can negate the 5-bit number 01101Here n=5, and 2n-1 = 3110 =Subtracting from yields 10010
32Two’s ComplementOur final idea is two’s complement. To negate a number, complement eachbit (just as for ones’ complement) and then add 1Examples:11012 = 1310 (a 4-bit unsigned number)= (a positive number in 5-bit two’s complement)= (a negative number in 5-bit ones’ complement)= (a negative number in 5-bit two’s complement)01002 = 410 (a 4-bit unsigned number)= +410 (a positive number in 5-bit two’s complement)= -410 (a negative number in 5-bit ones’ complement)= -410 (a negative number in 5-bit two’s complement)
33More about two’s complement Two other equivalent ways to negate two’s complement numbers:You can subtract an n-bit two’s complement number from 2nYou can complement all of the bits to the left of the rightmost 101101 = (a positive number in two’s complement)10011 = (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)(+1310)(-1310)(+410)(-410)
34Two’s Complement Addition Negating a two’s complement number takes a bit of work, but addition ismuch easier than with the other two systemsTo find A + B, you just have to:Do unsigned addition on A and B, including their sign bitsIgnore any carry outFor example, to find , or (+7) + (-4):First add as unsigned numbers:Discard the carry out (1)The answer is 0011 (+3)
35Another two’s complement example To further convince you that this works, let’s try adding two negativenumbers— , or (-3) + (-2) in decimalAdding the numbers gives 11011:Dropping the carry out (1) leaves us with the answer, 1011 (-5).
36Why does this work?For n-bit numbers, the negation of B in two’s complement is 2n - B (this isone of the alternative ways of negating a two’s-complement number)A - B = A + (-B)= A + (2n - B)= (A - B) + 2nIf A B, then (A - B) is a positive number, and 2n represents a carry out of1. Discarding this carry out is equivalent to subtracting 2n, which leaves uswith the desired result (A - B)If A B, then (A - B) is a negative number and we have 2n - (B - A). Thiscorresponds to the desired result, -(A - B), in two’s complement form.
37Comparing the signed number systems Positive numbers are the samein all three representationsSigned magnitude and one’scomplement have two ways ofrepresenting 0. This makesthings more complicatedTwo’s complement hasasymmetric ranges; there is onemore negative number thanpositive number. Here, you canrepresent -8 but not +8However, two’s complement ispreferred because it has onlyone 0, and its addition algorithmis the simplest
38Ranges of the signed number systems How many negative and positive numbers can be represented in each ofthe different systems on the previous page?In general, with n-bit numbers including the sign, the ranges are:
39Converting signed numbers to decimal Convert to decimal, assuming this is a number in:(a) signed magnitude format(b) ones’ complement(c) two’s complement
40Example solutionConvert to decimal, assuming this is a number in:Since the sign bit is 1, this is a negative number. The easiest way to find the magnitude is to convert it to a positive number.(a) signed magnitude formatNegating the original number, , gives , which is +21 in decimal. So must represent -21.(b) ones’ complementNegating in ones’ complement yields = +1010, so the original number must have been(c) two’s complementNegating in two’s complement gives = 1110, which means =The most important point here is that a binary number has differentmeanings depending on which representation is assumed
41Our four-bit unsigned adder circuit Here is the four-bit unsigned addition circuitNope, never saw itbefore in my life.
42Making a subtraction circuit We could build a subtraction circuit directly, similar to the way we madeunsigned addersHowever, by using two’s complement we can convert any subtraction probleminto an addition problem. Algebraically,A - B = A + (-B)So to subtract B from A, we can instead add the negation of B to AThis way we can re-use the unsigned adder hardware
43A two’s complement subtraction circuit To find A - B with an adder, we’ll need to:Complement each bit of BSet the adder’s carry in to 1The net result is A + B’ + 1, where B’ + 1 is the two’s complement negationof BRemember that A3, B3 and S3 here are actually sign bits
44Small differencesThe only differences between the adder and subtractor circuits are:The subtractor has to negate B3 B2 B1 B0The subtractor sets the initial carry in to 1, instead of 0Not too hard to make one circuit that does both addition and subtraction
45An Adder-Subracter Circuit XOR gates let us selectively complement the B inputX 0 = X X 1 = X’When Sub = 0, the XOR gates output B3 B2 B1 B0 and the carry in is 0.The adder output will be A + B + 0, or just A + BWhen Sub = 1, the XOR gates output B3’ B2’ B1’ B0’ and the carry in is 1.Thus, the adder output will be a two’s complement subtraction, A - B
46Signed OverflowWith two’s complement and a 4-bit adder, for example, the largestrepresentable decimal number is +7, and the smallest is -8What if you try to compute 4 + 5, or (-4) + (-5)?We cannot just include the carry out to produce a five-digit result, as for unsigned addition. If we did, (-4) + (-5) would result in +23!Also, unlike the case with unsigned numbers, the carry out cannot be usedto detect overflowIn the example on the left, the carry out is 0 but there is overflowConversely, there are situations where the carry out is 1 but there isno overflow(+4)(+5)(-7)(-4)(-5)(+7)
47Detecting signed overflow The easiest way to detect signed overflow is to look at all the sign bitsOverflow occurs only in the two situations above:If you add two positive numbers and get a negative resultIf you add two negative numbers and get a positive resultOverflow cannot occur if you add a positive number to a negative number.Do you see why?(+4)(+5)(-7)(-4)(-5)(+7)
48Sign ExtensionIn everyday life, decimal numbers are assumed to have an infinite numberof 0s in front of them. This helps in “lining up” numbersTo subtract 231 and 3, for instance, you can imagine:231- 003228You need to be careful in extending signed binary numbers, because theleftmost bit is the sign and not part of the magnitudeIf you just add 0s in front, you might accidentally change a negative numberinto a positive one!For example, going from 4-bit to 8-bit numbers:0101 (+5) should become (+5)But 1100 (-4) should become (-4)The proper way to extend a signed binary number is to replicate the signbit, so the sign is preserved
49Subtraction SummaryA good representation for negative numbers makes subtraction hardwaremuch easier to designTwo’s complement is used most often (although signed magnitude shows up sometimes, such as in floating-point systems)Using two’s complement, we can build a subtractor with minor changesto the adderWe can also make a single circuit which can both add and subtractOverflow is still a problem, but signed overflow is very different from theunsigned overflowSign extension is needed to properly “lengthen” negative numbers