40
CSCI N301: CSCI N301: Fundamental Computer Fundamental Computer Science Concepts Science Concepts Copyright Copyright ©2004 ©2004 Department of Computer & Information Science Department of Computer & Information Science Building Adders & Building Adders & Subtractors Subtractors

CSCI N301: Fundamental Computer Science Concepts Copyright ©2004 Department of Computer & Information Science Building Adders & Subtractors

Embed Size (px)

Citation preview

Page 1: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

CSCI N301:CSCI N301: Fundamental Computer Fundamental Computer Science ConceptsScience Concepts

Copyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Building Adders & Building Adders & SubtractorsSubtractors

Page 2: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

GoalsGoals

By the end of this lecture, you By the end of this lecture, you should understand…should understand…

• How to build a Half-AdderHow to build a Half-Adder• How to build a Full-AdderHow to build a Full-Adder• How to use Adders to perform How to use Adders to perform

subtractionsubtraction

Page 3: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

What We Can DoWhat We Can Do

• Last time, we learned how to build a circuit Last time, we learned how to build a circuit that met functional requirements.that met functional requirements.

• Today, let’s build a circuit that performs a Today, let’s build a circuit that performs a specific engineering function and supports specific engineering function and supports two output lines – we’ll build a circuit to two output lines – we’ll build a circuit to add two binary numbers …add two binary numbers …

Page 4: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Problem DefinitionProblem Definition

• We will start out simply, adding two single digit We will start out simply, adding two single digit binary numbersbinary numbers

• Let’s make certain we can do this by hand Let’s make certain we can do this by hand before we construct the circuitbefore we construct the circuit

• There are four possible combinations of single There are four possible combinations of single digit, two number addition:digit, two number addition:– 0 + 0 =00 + 0 =0– 0 + 1 = 10 + 1 = 1– 1 + 0 = 11 + 0 = 1– 1 + 1 = 101 + 1 = 10

Page 5: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Problem AnalysisProblem Analysis

• Notice that one of the combinations of Notice that one of the combinations of two single digit additions is a double two single digit additions is a double digit number, created by a carry:digit number, created by a carry:1 + 1 = 1 + 1 = 1100

Page 6: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Standardized OutputStandardized Output

• We’ll use leading zeros in order to produce a We’ll use leading zeros in order to produce a standard output:standard output:0 + 0 = 00 (0 plus 0 equals 0)0 + 0 = 00 (0 plus 0 equals 0)0 + 1 = 01 (0 plus 1 equals 1)0 + 1 = 01 (0 plus 1 equals 1)1 + 0 = 01 (1 plus 0 equals 1)1 + 0 = 01 (1 plus 0 equals 1)1 + 1 = 10 (1 + 1 equals 10)1 + 1 = 10 (1 + 1 equals 10)

• Notice that all of our answers include two bits. Notice that all of our answers include two bits. By putting the output in standardized form, we By putting the output in standardized form, we can stipulate the behavior of both outputs given can stipulate the behavior of both outputs given any combination of two inputs with the following any combination of two inputs with the following truth table …truth table …

Page 7: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Truth Table for Adding Truth Table for Adding Two 1-Bit NumbersTwo 1-Bit Numbers

INPUTSINPUTS OUTPUTSOUTPUTS

AA BB QQ RR

00 00 00 00

00 11 00 11

11 00 00 11

11 11 11 00

Page 8: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Breaking Down the Truth TableBreaking Down the Truth Table

• Notice the Q Notice the Q column (carry line) column (carry line) from the truth from the truth table? Recognize table? Recognize it? It’s an AND!:it? It’s an AND!:Q = A Q = A • B• B

INPUTSINPUTS OUTPUTSOUTPUTS

AA BB QQ RR

00 00 00 00

00 11 00 11

11 00 00 11

11 11 11 00

Page 9: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Breaking Down the Truth TableBreaking Down the Truth Table

• What about the R What about the R column (sum line)? column (sum line)? What sub-expression What sub-expression could we use? What could we use? What do you notice about do you notice about the R column?the R column?

• R is high iff one and R is high iff one and exactly one input is exactly one input is high …high …

INPUTSINPUTS OUTPUTSOUTPUTS

AA BB QQ RR

00 00 00 00

00 11 00 11

11 00 00 11

11 11 11 00

Page 10: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Breaking down the Truth TableBreaking down the Truth Table

• ““R will be high iff one and exactly one R will be high iff one and exactly one input is high.”input is high.”

• What expression can we use to prove What expression can we use to prove this statement?this statement?R = (~A R = (~A • B) + (A • ~B) • B) + (A • ~B)

Page 11: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Circuit DiagramCircuit Diagram

• Q = (~A Q = (~A • B) + (A • ~B)• B) + (A • ~B)

QQ

AAAA

BBBB

Page 12: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

A Short CutA Short Cut

• The circuit we created to diagram our R column The circuit we created to diagram our R column is so common in circuit design that it has a is so common in circuit design that it has a special name. It is called an Exclusive Or. It is special name. It is called an Exclusive Or. It is usually labeled XOR or drawn as a plus sign usually labeled XOR or drawn as a plus sign with a circle around it.with a circle around it.

• From our truth table, we can derive the following From our truth table, we can derive the following statement: “Our adding circuit should consist of statement: “Our adding circuit should consist of an XOR gate (for R output, the Sum line) and an an XOR gate (for R output, the Sum line) and an AND gate (for Q output, the Carry line). AND gate (for Q output, the Carry line).

Page 13: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

More on XORMore on XOR

• We have already established that any circuit can We have already established that any circuit can be created as a combination of 3 basic gates: be created as a combination of 3 basic gates: NOT, AND and OR.NOT, AND and OR.

• Although not one of the fundamental gates, the Although not one of the fundamental gates, the XOR gate is common enough that it is called a XOR gate is common enough that it is called a derived gatederived gate..

• We can build the XOR from the primitive gates. We can build the XOR from the primitive gates. However, engineers use the XOR so often However, engineers use the XOR so often they’ve given XOR its own symbol …they’ve given XOR its own symbol …

Page 14: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

XOR SymbolXOR Symbol

• The symbol for the XOR Gate:The symbol for the XOR Gate:

AA

BB

QQ

Q = A Q = A BB++++

Page 15: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Re-Writing the Truth TableRe-Writing the Truth Table

• The XOR circuit The XOR circuit delivers the delivers the Sum Line Sum Line (R) (R) of adding two single of adding two single digits.digits.

• The AND circuit The AND circuit produces the produces the Carry Line Carry Line (Q)(Q). .

• We’ve just created a We’ve just created a fundamental circuit fundamental circuit called a called a Half AdderHalf Adder, , which provides the which provides the capability of adding two capability of adding two single bit numbers.single bit numbers.

INPUTSINPUTS OUTPUTSOUTPUTS

AA BBQQ RR

AA•B•B A BA B

00 00 00 00

00 11 00 11

11 00 00 11

11 11 11 00

++++

Page 16: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Circuit Diagram for the Half AdderCircuit Diagram for the Half Adder

AA

BB

RRSUMSUM

QQCARRYCARRY

Page 17: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Using the Half-Adder as a Using the Half-Adder as a Building BlockBuilding Block

• The Half-Adder circuit is more powerful than you The Half-Adder circuit is more powerful than you might think. It operates as the key component in might think. It operates as the key component in a divide-and-conquer approach to adding a divide-and-conquer approach to adding nn digits.digits.

• How does this work? The first leap of faith How does this work? The first leap of faith comes in realizing that three single digit comes in realizing that three single digit numbers could be added two at a time: numbers could be added two at a time: – First, add the first two digits together using a half-adder.First, add the first two digits together using a half-adder.– Then, add the result of this sum to the third number using Then, add the result of this sum to the third number using

another half-adder.another half-adder.

Page 18: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Full AdderFull Adder

• Let’s try to add 3 single digit numbers by Let’s try to add 3 single digit numbers by stringing together two half-adders:stringing together two half-adders:– We need 3 input lines, one for each number.We need 3 input lines, one for each number.– Two input lines feed into the first half adder; the third input line Two input lines feed into the first half adder; the third input line

functions as a carry-in line. functions as a carry-in line. – The second half-adder has two input lines: the output sum from The second half-adder has two input lines: the output sum from

the first half-adder, plus the third, carry-in input line.the first half-adder, plus the third, carry-in input line.

• The next slides show the truth table for the Full The next slides show the truth table for the Full Adder and then the circuit diagram for the Full Adder and then the circuit diagram for the Full Adder …Adder …

Page 19: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Truth Table for the Full AdderTruth Table for the Full Adder

INPUTSINPUTS OUTPUTSOUTPUTS

AA BB CCININ QQCARRYCARRY RRSUMSUM

00 00 00 00 00

00 00 11 00 11

00 11 00 00 11

00 11 11 11 00

11 00 00 00 11

11 00 11 11 00

11 11 00 11 00

11 11 11 11 11

Page 20: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Circuit Diagram for the Full AdderCircuit Diagram for the Full Adder

AA

BB

RRSUMSUM

QQCARRYCARRY

CCININ

Page 21: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Shorthand Notation Shorthand Notation for the Full Adderfor the Full Adder

• The full adder is usually drawn in a The full adder is usually drawn in a shorthand notation:shorthand notation:

FULLFULLADDERADDER

AA

BB

CCININ

QQCARRYCARRY

RRSUMSUM

Page 22: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Interpreting the Full Adder CircuitInterpreting the Full Adder Circuit

• Do you see any patterns between the output lines Do you see any patterns between the output lines and the input lines?and the input lines?

• If you look at the truth table, you can see that the If you look at the truth table, you can see that the QQCARRYCARRY output line is true output line is true if and only ifif and only if exactly two or exactly two or three input lines are high.three input lines are high.

• Also, the Also, the RRSUMSUM output line is high when the XOR of A output line is high when the XOR of A and (the sum of Input B and Cand (the sum of Input B and C ININ) is high? In other ) is high? In other words, add the Input B and Cwords, add the Input B and C ININ values together, as values together, as though you were putting them through a half adder. though you were putting them through a half adder. Take the result, and XOR it with the value of the A Take the result, and XOR it with the value of the A input line. The result is the value for the input line. The result is the value for the RRSUMSUM output output lineline

Page 23: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Summarizing the Full AdderSummarizing the Full Adder

• By connecting two half-adders By connecting two half-adders together, we can add 3 single digit together, we can add 3 single digit binary numbersbinary numbers

• It shouldn’t come as a surprise to It shouldn’t come as a surprise to learn that by stringing even more half-learn that by stringing even more half-adders together, we can add even adders together, we can add even more single digit numbers…more single digit numbers…

Page 24: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

The The NN-Bit Adder-Bit Adder

• The next leap of faith is to move from adding 3 (or The next leap of faith is to move from adding 3 (or more) single digit numbers to adding multi-bit more) single digit numbers to adding multi-bit numbersnumbers

• First, note that the sum of adding two N-bit numbers First, note that the sum of adding two N-bit numbers can be N + 1 bitscan be N + 1 bits

• This comes as a result of possibly obtaining a carry This comes as a result of possibly obtaining a carry into the next columninto the next column

• Adding N-bit numbers isn’t an abstract consideration Adding N-bit numbers isn’t an abstract consideration – it’s a – it’s a realreal computing operation - at a minimum, a computing operation - at a minimum, a modern computer would add 16 bit numbers, done modern computer would add 16 bit numbers, done with a 16-bit adderwith a 16-bit adder

Page 25: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

The The NN-Bit Adder-Bit Adder

• Here is the general rule: to add two N-bit Here is the general rule: to add two N-bit numbers, N full adders are required, one for numbers, N full adders are required, one for each column in the sumeach column in the sum

• The first 2 inputs for each full adder come from The first 2 inputs for each full adder come from the digits in the numbers to be addedthe digits in the numbers to be added

• The carry-out produced for each column is used The carry-out produced for each column is used as the carry-in for the next column to the leftas the carry-in for the next column to the left

• At some point, you can have overflow as you At some point, you can have overflow as you exceed the adder circuitry capability.exceed the adder circuitry capability.

Page 26: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Getting RealGetting Real

• The processing component of the ALU – the The processing component of the ALU – the arithmetic logic unit – uses Adder circuitry to arithmetic logic unit – uses Adder circuitry to perform fundamental arithmetic operations.perform fundamental arithmetic operations.

• By connecting half adders to make a full adder, By connecting half adders to make a full adder, and multiple full adders in a chain, we now can and multiple full adders in a chain, we now can work our way to simple addition.work our way to simple addition.

• Other operations are a variation on this Other operations are a variation on this fundamental capability …fundamental capability …

Page 27: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

How to SubtractHow to Subtract

• What other arithmetic operations do we need to be able What other arithmetic operations do we need to be able to perform with circuits?to perform with circuits?

• The next logical step would be to tackle subtraction …The next logical step would be to tackle subtraction …• We could come up with a separate subtraction circuit, We could come up with a separate subtraction circuit,

and some clever scheme of knowing which circuit to and some clever scheme of knowing which circuit to activate based on what kind of math problem we activate based on what kind of math problem we encounteredencountered

• It turns out that the solution is more elegant -- it uses the It turns out that the solution is more elegant -- it uses the approach we have seen repeatedly: turn a new problem approach we have seen repeatedly: turn a new problem into a problem we have already solved …into a problem we have already solved …

Page 28: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

From Subtraction to AdditionFrom Subtraction to Addition

• What we have is a circuit that can add numbers. What we have is a circuit that can add numbers. We want to make this circuit also work for We want to make this circuit also work for subtracting two numbers…subtracting two numbers…

• Let’s rethink our problem, with a specific Let’s rethink our problem, with a specific example: 5 - 2 = 3, or, put another way:example: 5 - 2 = 3, or, put another way:5 + (-2) = 35 + (-2) = 3

• Now, we have an addition problem, instead of Now, we have an addition problem, instead of subtraction. This means our existing circuit will subtraction. This means our existing circuit will work we could unlock how to represent negative work we could unlock how to represent negative numbers, like -2.numbers, like -2.

Page 29: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Two’s ComplementTwo’s Complement

• Remember that we can represent negative Remember that we can represent negative numbers using a scheme called Two’s numbers using a scheme called Two’s Complement.Complement.

• In two’s complement, to encode a negative In two’s complement, to encode a negative number, you take its positive value, number, you take its positive value, complement it, and then add one.complement it, and then add one.

• The resulting number will be in two’s The resulting number will be in two’s complement form.complement form.

Page 30: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Review of Two’s ComplementReview of Two’s Complement

1.1. Convert each number from Base-10 Convert each number from Base-10 to its Base-10 format (we’ll use four-to its Base-10 format (we’ll use four-bit numbers); for negative values, bit numbers); for negative values, convert their absolute value:convert their absolute value:551010 = 0101 = 010122

|-2|-21010|| = 0010= 001022

Page 31: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Review of Two’s ComplementReview of Two’s Complement

2.2. Complement the negative value, Complement the negative value, changing all 1s to 0s and all 0s to changing all 1s to 0s and all 0s to 1s. The result is called a 1s. The result is called a One’s One’s ComplementComplement::001000102 2 1101 110122

Page 32: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Review of Two’s ComplementReview of Two’s Complement

3.3. Add 1 to the One’s Complement. Add 1 to the One’s Complement. The result is your original negative The result is your original negative number in Two’s Complement form:number in Two’s Complement form:-2-21010 0010 00102 2 1101 11012 2 + 1+ 122 = 1110 = 111022

Page 33: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Problem SolutionProblem Solution

• Then, we add the first number of our problem (Then, we add the first number of our problem (551010 or or

0101010122) to this two’s complemented number: ) to this two’s complemented number:

0101010122 + 1110 + 111022 = 10011 = 1001122

• If we throw away the carry line (the leftmost 1), our If we throw away the carry line (the leftmost 1), our answer is correct: answer is correct: 0011001122 = 3 = 31010

• Is that legal? Why or why not? We are restricting Is that legal? Why or why not? We are restricting ourselves here to 2, 4-bit numbers. We can choose ourselves here to 2, 4-bit numbers. We can choose to simply ignore the carry line (the 5to simply ignore the carry line (the 5 thth bit). bit).

Page 34: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Building It with CircuitsBuilding It with Circuits

• Can we make this fly with our circuitry?Can we make this fly with our circuitry?• Let’s map each step… Let’s map each step… • The first step was to create the two’s The first step was to create the two’s

complement of the number we subtracted.complement of the number we subtracted.• We did this in a two-step process: first we took We did this in a two-step process: first we took

the one’s complement, then added one to get the one’s complement, then added one to get the final two’s complement.the final two’s complement.

Page 35: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

The Circuitry of Two’s ComplementThe Circuitry of Two’s Complement

• How would we take one’s complement?How would we take one’s complement?– A NOT gate would work here…. Any number you put in, the A NOT gate would work here…. Any number you put in, the

NOT gate would reverseNOT gate would reverse

• The next step would be to add one… we could The next step would be to add one… we could use an addition circuit, to make certain one was use an addition circuit, to make certain one was added.added.

• Now we have the two’s complemented number, Now we have the two’s complemented number, and it is combined with the first number in an and it is combined with the first number in an adder circuit to produce an answer.adder circuit to produce an answer.

Page 36: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Pretty ClosePretty Close

• This is pretty close to how subtraction is actually This is pretty close to how subtraction is actually performed in a computer.performed in a computer.

• There are a few tweaks for efficiency and There are a few tweaks for efficiency and minimal circuit designminimal circuit design

• The second step of two’s complement requires The second step of two’s complement requires you to add one, and we could do this with an you to add one, and we could do this with an adder. However, do you remember how we built adder. However, do you remember how we built our multi-bit adder by stringing together multiple our multi-bit adder by stringing together multiple adders?adders?

Page 37: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

4-bit Full Adder4-bit Full Adder

• Note that the bottom full Note that the bottom full adder in this 4 bit adder has adder in this 4 bit adder has an unused carry in line (at an unused carry in line (at the bottom of the picture). If the bottom of the picture). If we make this line high, it we make this line high, it would have the same effect would have the same effect as adding one… and that is as adding one… and that is how the add one part of two’s how the add one part of two’s complement is achieved. complement is achieved. This cuts down on the This cuts down on the needed circuitry for needed circuitry for subtraction.subtraction.

Page 38: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Summary of SubtractorsSummary of Subtractors

• Represent the subtraction problem as an Represent the subtraction problem as an addition problem by adding a negative value addition problem by adding a negative value of the number.of the number.

• We can represent negative numbers using We can represent negative numbers using Two’s Complement.Two’s Complement.

• Once we have converted the subtraction Once we have converted the subtraction problem to an addition problem, we can use problem to an addition problem, we can use an adder, built from a half-adder consisting of an adder, built from a half-adder consisting of an And and an XOR gate.an And and an XOR gate.

Page 39: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Subtraction: The RuleSubtraction: The Rule

• By using the two’s complement approach, By using the two’s complement approach, the following rule applies for performing the following rule applies for performing the arithmetic operation of subtraction:the arithmetic operation of subtraction:“X – Y can be computed by adding the “X – Y can be computed by adding the negative of Y to X using addition circuitry negative of Y to X using addition circuitry designed for positive numbers, and designed for positive numbers, and ignoring any carry-out that is produced.”ignoring any carry-out that is produced.”

Page 40: CSCI N301: Fundamental Computer Science Concepts Copyright ©2004  Department of Computer & Information Science Building Adders & Subtractors

N301: Fundamental Computer Science ConceptsN301: Fundamental Computer Science ConceptsCopyright Copyright ©2004 ©2004 Department of Computer & Information ScienceDepartment of Computer & Information Science

Questions?Questions?