Docsity

Binary Addition Assignment for Math 248, Spring 2003 - Prof. Charles Pruett, Assignments of Mathematics

Toggle sidebar
bg1
Alternate Programming Assignment 2
Math 248/Spring 07/Pruett
(Compliments of P. Warne, Adapted)
An important function of digital computers is the ability to use logical functions to perform
arithmetic. The foundation of this is addition; if two binary numbers can be added, then they
can just as easily be subtracted. Once we can add and subtract, we can then create an
algorithm built on this foundation to multiply and divide. How, then, do we add two binary
numbers?
We will start simple and just consider adding together two binary bits. Since each bit has only
two possible values, 0 or 1, there are only four ways these can be combined. These four
possibilities, and the resulting sums in binary, are:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10 (= 2 in base 10)
That fourth line though indicates that we need to deal with the possibility of having two
output bits when we add our two input bits: the bit to the right which we call a “sum bit” and
a possible bit to the left which we call a “carry bit”. However this is analogous to what we do
in base 10 when we add two decimal digits 0,1,2,…,9. For example:
4 + 3 = 7 which does not require a second output “carry” digit
6 + 8 = 14 which does require a second output “carry” digit
Now, read section 3.5 pp. 63-65 in an Introduction to Fortran 90 (copied for
you) to get a better understanding of how this simple addition of single bit
plus single bit could be accomplished by a digital computer using a “half-
adder”. C-LEVEL ASSIGNMENT: Encode and test both logical and
binary half-adders.
We will certainly want to be able to add more than just two bits together. The digital
computer would be of little use if it could only add 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1,
and 1 + 1 = 10. How can we create a circuit that would add two binary numbers of let’s say
23 bits each? This seems, at first, a little overwhelming so let’s just consider adding two binary
numbers of 2 bits each. We want to add A2A1 + B2B1, where A1,A2 ,B1,B2 {0,1}, for example
10+11, now how to do this? (And don’t even think about answering “21”!) First we will make
sure that we understand what the actual answers would be to make sure that our algorithm is
correct, by listing the numbers 1 through 8 in base 10 in binary:
0 = 0
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 1000
MATH 248,Spring 2003
pf2

Partial preview of the text

Download Binary Addition Assignment for Math 248, Spring 2003 - Prof. Charles Pruett and more Assignments Mathematics in PDF only on Docsity!

Alternate Programming Assignment 2

Math 248/Spring 07/Pruett

(Compliments of P. Warne, Adapted)

An important function of digital computers is the ability to use logical functions to perform arithmetic. The foundation of this is addition; if two binary numbers can be added, then they can just as easily be subtracted. Once we can add and subtract, we can then create an algorithm built on this foundation to multiply and divide. How, then, do we add two binary numbers? We will start simple and just consider adding together two binary bits. Since each bit has only two possible values, 0 or 1, there are only four ways these can be combined. These four possibilities, and the resulting sums in binary, are: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 (= 2 in base 10) That fourth line though indicates that we need to deal with the possibility of having two output bits when we add our two input bits: the bit to the right which we call a “sum bit” and a possible bit to the left which we call a “carry bit”. However this is analogous to what we do in base 10 when we add two decimal digits 0,1,2,…,9. For example: 4 + 3 = 7 which does not require a second output “carry” digit 6 + 8 = 14 which does require a second output “carry” digit

Now, read section 3.5 pp. 63-65 in an Introduction to Fortran 90 (copied for

you) to get a better understanding of how this simple addition of single bit

plus single bit could be accomplished by a digital computer using a “half-

adder”. C-LEVEL ASSIGNMENT: Encode and test both logical and

binary half-adders.

We will certainly want to be able to add more than just two bits together. The digital computer would be of little use if it could only add 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10. How can we create a circuit that would add two binary numbers of let’s say 23 bits each? This seems, at first, a little overwhelming so let’s just consider adding two binary numbers of 2 bits each. We want to add A 2 A 1 + B 2 B 1 , where A1,A 2 ,B1,B 2 {0,1}, for example 10+11, now how to do this? (And don’t even think about answering “21”!) First we will make sure that we understand what the actual answers would be to make sure that our algorithm is correct, by listing the numbers 1 through 8 in base 10 in binary: 0 = 0 1 = 1 2 = 10 3 = 11 4 = 100 5 = 101 6 = 110 7 = 111 8 = 1000 MATH 248,Spring 2003

Now, read exercise 18, and then exercise 19 on pp. 173-175 in the old (larger)

Fortran 90 book (on reserve in the Carrier Library) to get a little better

understanding of how addition of two double bit binary numbers could be

accomplished by a digital computer using essentially three “half- adders.”

Note that the program Half_Adder on p. 171 uses LOGICAL variables and data, and this works exceptionally well given the framework of logical expressions and operators, but the program could be re-written using a few more IF-THEN’s to use INTEGER variables and data in conjunction with logical expressions and operators. This would only be done if the user was just more comfortable with INTEGER vs. LOGICAL variables and data. Take a look at the logic circuitry at http://www.math.jmu.edu/~warnepg/wordbox/M248F01/PA2adder.doc

B-LEVEL ASSIGNMENT: Implement either a logical or a binary full adder

for 2-bit integers.

A-LEVEL ASSIGNMENT: Implement either a logical or a binary full adder

for 8-bit integers. Be sure to include enough output to convince me that your

add algorithm works correctly.

MATH 248,Spring 2003