Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Addition and Subtraction

Similar presentations


Presentation on theme: "Binary Addition and Subtraction"— Presentation transcript:

1 Binary Addition and Subtraction
CS/COE 0447 (term 2181) Jarrett Billingsley

2 Class announcements hi :) how did you feel about the exam
how's the project goin BREAK THINGS UP INTO FUNCTIONS, OH MY LORD let's look at mine to refresh your memory on how it's supposed to look/work 10/3/2017 CS/COE term 2181

3 Binary arithmetic 10/3/2017 CS/COE term 2181

4 What makes a good word size?
Can you think of an example of… 100 of something? a million of something? a billion? a quintillion (1018)? more? 28 = 256, 216 ≅ 65,000, 232 ≅ 4 billion, 264 ≅ 18 quintillion But for a given word size, all the circuitry has to be able to support that many bits. Tradeoffs, tradeoffs. That's engineering. 10/3/2017 CS/COE term 2181

5 Remember this? Explain to me, step-by-step, how to add these two binary numbers. 8/31/2017 CS/COE term 2181

6 Formalizing the algorithm
For each pair of bits starting at the LSB, Add the two bits and the carry. The low bit of the sum goes into the sum row. The high bit of the sum is the carry for the next higher bit. This is known as the grade school algorithm, cause it's how you learned to add in grade school! 10/3/2017 CS/COE term 2181

7 A B C S 1 1 1 1 1 1 1 The Tables of Truth
Let's try to come up with a truth table for adding two bits. Each column will hold 1 bit. Let's name the outputs C and S, for Carry and Sum. Let's name the inputs A and B. A B C S Now let's fill in the output values. For the input values, we kinda count up in binary. 1 1 Hey, this C column looks familiar… so does the S column. 1 1 1 1 1 Great! But this is wrong. 10/3/2017 CS/COE term 2181

8 00111 110 1011 0010 +0010 1111 1110 0001 Co Ci A B S Half-truth tables
What we just made was a half-adder. It produces a carry output but doesn't account for the carry input. To make a full adder, we need 3 input bits. To clarify… Ci A B Co S 1 1 Co Ci 1 1 1 1 1 A 1 1 B 1 1 1 1 1 1 S 1 1 1 1 1 10/3/2017 CS/COE term 2181

9 The logic of it all Ci A B Co S 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
It looks a little messy, but it kinda makes sense if you think of it like this: It counts how many input bits are "1". Co and S are a 2-bit number! If we look at the outputs in isolation: S is 1 if we have an odd number of "1s". Co is 1 if we have 2 or 3 "1s". It's a little weird, but we can build this out of AND, OR, and XOR gates. Ci A B Co S 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10/3/2017 CS/COE term 2181

10 Sweeping that under the rug…
In programming, we use functions to be able to reuse code. In hardware, we can group these 5 gates into a component. Here's the symbol for a one-bit full adder. + B A Ci Co S The outputs are analogous to return values. Now we don't have to care how it adds, just that it does. The inputs are analogous to parameters/arguments. 10/3/2017 CS/COE term 2181

11 Adding longer numbers 10/3/2017 CS/COE term 2181

12 1 0 1 1 0 0 1 0 +0 0 1 0 1 1 1 1 Where do the carries go?
When you add one place, you might get a carry out. That then becomes the carry in for the next higher place. Bit Bucket..? 10/3/2017 CS/COE term 2181

13 + + + + A2 A1 A0 B2 B1 B0 S2 S1 S0 Ripple Carry A2 S2 B2 A1 S1 B1 A0
If we want to add two three-bit numbers, we'll need three one-bit adders. We chain the carries from each place to the next higher place, like we do on paper. We have to split the numbers up like so: + B2 A2 S2 + B1 A1 S1 A2 A1 A0 B2 B1 B0 S2 S1 S0 + B0 A0 S0 + 10/3/2017 CS/COE term 2181

14 Gate Delay Electrical signals can't move infinitely fast.
The transistors can't turn on and off infinitely fast. Since each digit must wait for the next smaller digit to compute its carry, ripple carry will take time linear in the number of digits. This is a diagram of how the outputs of a 16-bit ripple carry adder change over time (courtesy It's measured in picoseconds! So about 100ps for the carries to ripple all the way through. But if we went to 32 bits, it'd take 200ps. And 64 bits, 400ps... etc. There are more efficient ways of adding. 10/3/2017 CS/COE term 2181

15 How many bits? 99 +99 198 9999 +9999 19998 If you add two 2-digit decimal numbers, what's the largest number you can get? What about two 4-digit decimal numbers? What about two 4-bit numbers? What's the pattern of the number of digits? If you add two n-digit numbers in any base, the result will have at most n + 1 digits. That means if we add two 32-bit numbers… …we might get a 33-bit result! If we have more bits than we can store in our number, that's overflow. 1111 +1111 11110 10/3/2017 CS/COE term 2181

16 Detecting overflow 10/3/2017 CS/COE term 2181

17 Unsigned overflow What could we do with that 33rd bit if it's a 1?
We could ignore it. This is usually a bad idea. You added two numbers and got an incorrect result. Now your program is broken. This is what addu in MIPS does – u for "unchecked" or "unsafe." We could cause an exception. This is what add in MIPS does – normally it crashes the program, but you can handle it yourself, too. We could put that 33rd bit somewhere else. Many other architectures do this, by putting it into a "carry bit" register, which can then be checked by the program. This is very useful for "arbitrary precision arithmetic" – if you want to add 2048-bit numbers, chain many 32-bit additions! 10/3/2017 CS/COE term 2181

18 Signed overflow -2 -3 +1 +2 +3 -1 -4 110 101 000 001 010 011 111 100
Remember that the signed number line looks like this: Basically, overflow occurs when we go off either end of the number line. In unsigned arithmetic, gives 10002, so that's an overflow. But in signed arithmetic, gives 0, which is fine! Detecting signed overflow is a bit more subtle… When you go off the right end of the number line, what kind of answer do you get? What about the left end? 110 -2 101 -3 000 001 +1 010 +2 011 +3 111 -1 100 -4 10/3/2017 CS/COE term 2181

19 Detecting signed overflow
If you add two numbers of different signs (one negative, one positive), is it ever possible to go off the ends of the number line? No! In that case, you are always getting closer to 0. If you add two numbers of the same sign, how do you know there's an overflow? If you add two positive numbers and get a negative number. If you add two negative numbers and get a positive number. How do you check the signs of the three numbers? (It's easy!) 10/3/2017 CS/COE term 2181

20 Subtracting in binary? 10/3/2017 CS/COE term 2181

21 Flip side We could come up with a separate subtraction circuit, but… Algebra tells us that x - y = x + (-y) Negation means flip the bits and add 1. Flipping the bits uses NOT gates. How do we add the 1? We use a full adder for the LSB, and when we're subtracting, set the "carry in" to 1! + ~B1 A1 S1 + ~B0 A0 S0 1 10/3/2017 CS/COE term 2181


Download ppt "Binary Addition and Subtraction"

Similar presentations


Ads by Google