0% found this document useful (0 votes)
86 views13 pages

Arith PDF

The document discusses binary arithmetic, including: 1) Adding and subtracting in binary works the same way as in decimal, with carries and borrows in powers of 2 instead of 10. 2) A full adder can be implemented as a circuit to add two bits along with a carry-in bit. 3) Multiple full adders can be connected to build an n-bit adder by connecting the carry-out of each stage to the next stage's carry-in. This allows adding numbers with more than one bit.

Uploaded by

JC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views13 pages

Arith PDF

The document discusses binary arithmetic, including: 1) Adding and subtracting in binary works the same way as in decimal, with carries and borrows in powers of 2 instead of 10. 2) A full adder can be implemented as a circuit to add two bits along with a carry-in bit. 3) Multiple full adders can be connected to build an n-bit adder by connecting the carry-out of each stage to the next stage's carry-in. This allows adding numbers with more than one bit.

Uploaded by

JC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Cmpt 150 Binary Arithmetic February, 2012

Binary Arithmetic
e Arithmetic in binary is really no different than arithmetic in decimal, except
that we only have two legal digits instead of ten.

e Let’s do 5+7 in decimal and in binary.

f In decimal, the operation looks something like this (with carries shown
in ‘()’ above each digit position):
(1)
5
7
1 2
If the sum of the digits in a column exceeds 9, we carry (in units of
powers of 10) into the next column.
f In binary, the operation looks something like this:
(1) (1) (1)
1 0 1
1 1 1
1 1 0 0
If the sum of the digits in a column exceeds 1, we carry (in units of
powers of 2) into the next column.

e The principles are identical: you add up the digits in the column, and you
get a sum digit and a carry digit. But since we’re in binary, the sum is 0 or
1, and the carry is 0 or 1.

e If we were working in hexadecimal, we wouldn’t have any carry at all, be-


cause 5 + 7 = c, and we only need one digit to express the result. Only when
the sum of digits in a column exceeds 15 do we have a carry when we work
in hexadecimal.

e If we look at subtraction, the same rules apply again.

f When we subtract in decimal, if the minuend is smaller than the sub-


trahend, we borrow (in units of powers of 10) from the next column.
(1)
1 2
7
5

1
Cmpt 150 Binary Arithmetic February, 2012

f When we subtract in binary, if the minuend is smaller than the subtra-


hend, we borrow (in units of powers of 2) from the next column.
(1) (1) (1)
1 1 0 0
1 1 1
1 0 1

e How do we implement arithmetic? Just the way we’d implement any other
boolean function on 3 variables. (For addition, we’ve already done half the
work, as the example we used to learn K-map techniques.)

e Start with a truth table showing the inputs and outputs.

x(i) y(i) ci(i) Σ (i) co(i)


0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1

e The K-maps and boolean expressions for Σ (i) and co(i) are:
xy xy
ci 00 01 11 10 ci 00 01 11 10

0 0 1 0 1 0 0 0 1 0

1 1 0 1 0 1 0 1 1 1

Σ (i) = x(i) ⊕ y(i) ⊕ ci(i) co(i) = x(i) ⋅ y(i) + x(i) ⋅ ci(i) + y(i) ⋅ ci(i)
The K-map looks pretty hopeless
until you remember that this is
the pattern for XOR.
e The circuit for a full adder is:

2
Cmpt 150 Binary Arithmetic February, 2012

x(i)

y(i)

co(i)
ci(i)

Σ(i)

e This is well and good — we can add up two bits. But to make something
really useful, we’ll want numbers greater than 0 or 1. How do we go about
this?

e The full adder that we’ve made is just a single-bit slice of an n-bit adder. If
we tie the carry-out of stage i to the carry-in of stage i + 1, we can create an
adder for as many bits as we like.

e Suppose I rearrange the pieces of the full adder to make that a little more
convenient:
x(i) y(i)

co(i)
ci(i)

Σ(i)

e Then I can connect four single-bit slices in a ripple configuration to make a


four-bit adder:

3
Cmpt 150 Binary Arithmetic February, 2012

x(i+3) y(i+3) x(i+2) y(i+2) x(i+1) y(i+1) x(i) y(i)

co(i+3) ci(i)

Σ(i+3) Σ(i+2) Σ(i+1) Σ(i)

e Generally, we won’t keep the inner detail in view — the idea is to begin
creating a hierarchy of abstractions.
f Just as in programming, really. It’s analogous to creating a subroutine
in a programming language.
e The first step is to wrap up the full adder logic schematic in a box, showing
just the inputs and outputs, so that we have:

x y
FA
co ci

e Then we can string the boxes together to make a four-bit adder:


x(i+3) y(i+3) x(i+2) y(i+2) x(i+1) y(i+1) x(i) y(i)

x y x y x y x y
FA FA FA FA
co(i+3) co ci co ci co ci co ci ci(i)
Σ Σ Σ Σ

Σ(i+3) Σ(i+2) Σ(i+1) Σ(i)

e The final step is to take the four full adder components and reduce them to
a single component, representing a four-bit adder:

4 4
x(3:0) y(3:0)
co ci
adder
Σ(3:0)

4
Cmpt 150 Binary Arithmetic February, 2012

f A common way to indicate a multi-bit data path is shown in the figure:


a slash through the line, with a number beside it to show how many
bits are in the connection.

e So, now we can add two unsigned (positive) numbers. How do we extend
what we know to handle subtraction?

e We could do the obvious thing — work up a truth table for subtracting in


binary.

f It would look like this:


xi yi bii (xi − yi − bi i ) boi
0 0 0 0 0
0 0 1 1 1
0 1 0 1 1
0 1 1 0 1
1 0 0 1 0
1 0 1 0 0
1 1 0 0 0
1 1 1 1 1
f The k-maps and boolean expressions are
xy xy
bi 00 01 11 10 bi 00 01 11 10

0 0 1 0 1 0 0 1 0 0

1 1 0 1 0 1 1 1 1 0

diff (i) = x(i) ⊕ y(i) ⊕ bi(i) bo(i) = x(i) ⋅ y(i) + x(i) ⋅ bi(i) + y(i) ⋅ bi(i)
e But now we have a problem — how do we know if the result is positive
or negative? And that begs the question, “How do we represent a negative
number?”

e We could continue to handle this by analogy to the way we do decimal arith-


metic.

f In decimal, we put a sign (+ or −) in front of the number to indicate


whether it’s positive or negative. This is signed magnitude representa-
tion.
f That’s only two symbols, so we could prepend an extra bit to all our
binary numbers, and adopt the convention that if this first bit is 0,
the remainder of the number is positive, and if the first bit is 1, the
remainder of the number is negative.

5
Cmpt 150 Binary Arithmetic February, 2012

f Now, when we come to do addition or subtraction, we have to check the


first bit to see if we should actually add or subtract (because adding a
negative number means subtraction, and subtracting a negative number
means addition). And we’ll need to be able to do either of x − y or y − x,
because the addition (−x) + y is equivalent to y − x.
f Or, we could put multiplexers at the inputs to our add/subtract logic, so
that we could swap x and y when necessary.
f This is getting complicated — we’ve got lots of decisions to make, and
the amount of gates we’ll need to implement all this is piling up.

6
Cmpt 150 Binary Arithmetic February, 2012

Two’s Complement Representation


e It turns out that we can represent negative numbers in a way that’s much
easier to work with than signed magnitude, using a representation called
two’s complement.

e So that this doesn’t come across as complete magic, let me give a simple
illustration in decimal, before I give you the rules in binary.

f The general idea here is something called the radix complement. For
decimal, that’d be 10’s complement.
f To form the 10’s complement of a number, you subtract the number
from the next higher power of 10. E.g., the 10’s complement of 209 is
1000 − 209 = 791.
For future reference, note that I could write −209 = −1000 + 791.
f Now, suppose I want to perform the subtraction 315 − 209. You’d agree
with me that 315 − 1000 + 1000 − 209 is an equivalent operation.
f So, I have 315 − 209 = 315 + 791 − 1000 = 1106 − 1000 = 106, which is
the correct answer.

e Right, you say. I’ve taken a relatively simple operation and doubled the work.
Trust me, it’ll look better in binary.

e The first thing we need to know is how to form the 2’s complement of a
binary number.

f It’s the same procedure, we subtract the number from the next higher
power of 2. E.g., to form the 2’s complement of 4310 = 1010112 , we
perform the subtraction 1000000 − 101011 = 010101.
f This isn’t too helpful, though, because it still involves subtraction. But,
suppose I make the following observation: 1000000 − 0000001 = 0111111,
and 1 − 0 = 1, 1 − 1 = 0, i.e., logical NOT.
So, if I perform ((1000000 − 1) − 101011) + 1 = ¬101011 + 1 = 010101, I
can see that the easy rule to form the two’s complement of a binary
number is “NOT(number) + 1”.
To repeat the previous example again, I can form the 2’s complement of
4310 as ¬101011 + 1 = 010100 + 1 = 010101.

e Now, for the big test — can I do subtraction in binary using two’s comple-
ment in the same way that I did in decimal with 10’s complement?

e Let’s try 5010 − 4310 = 710 .

7
Cmpt 150 Binary Arithmetic February, 2012

e That becomes 110010 − 1000000 + (1000000 − 101011) = 110010 − 1000000 +


010101. Rearranging a bit, we have 110010 + 010101 − 1000000 = 1000111 −
1000000 = 111, which is the correct answer.

e Now we come to a really sticky problem. Up to now, I’ve been ignoring


the question of just how many bits I’m using to represent a number. I’ve
been using as many bits as seemed appropriate at the time, and I’ve been
interpreting results to fit the context of the arithmetic operation. This won’t
do.
Consider: Both 50 and 43 can be represented in 6 binary digits. The same
for 21 (0101012 ).
But to do the calculation 50 − 43, I really used 7 bits. The 7th bit was needed
because the result of adding 110010 + 010101 = 1000111 required 7 bits,
and then we needed to subtract 10000002 .
It’s tempting to say, oh, just handle the final bit implicitly.
To which I reply, how do you know whether the calculation 110010 + 010101
represents 50 − 43 = 7 or 50 + 21 = 71?
In the former case, the correct answer is 7, which can be represented in 6
bits, and I’m ok.
In the latter case, the correct answer is 71, which is 1000111, 7 bits are
required to represent it, and I’m in trouble.

e The answer is to explicitly include the power of two used to form the radix
complement as part of the representation of a binary number.

f Remember I mentioned that we could write −209 as −1000 + 761.


f We’re going to do an analogous thing for two’s complement numbers.
The key is this: When representing two’s complement numbers in a com-
puter, the most significant bit will have a negative weight. This is a large
step, so we’ll go a bit slowly here.
f Let’s consider 7 bit numbers. The weight attached to the digits will be
as follows:
−26 25 24 23 22 21 20
f To represent the number +2110 = 00101012 , I’d have
−26 25 24 23 22 21 20
0 0 1 0 1 0 1
16 +4 +1
f To represent the number −4310 = 10101012 , I’d have

8
Cmpt 150 Binary Arithmetic February, 2012

−26 25 24 23 22 21 20
1 0 1 0 1 0 1
-64 +16 +4 +1

e What about range? Let’s explore the limits of what we can represent with 7
bits.

f Consider the number 00000002 . Clearly, this is still 010 .


f What about 01111112 ? We have
−26 25 24 23 22 21 20
0 1 1 1 1 1 1
+32 +16 +8 +4 +2 +1
for a value of 63.
f What about 10000002 ? We have
−26 25 24 23 22 21 20
1 0 0 0 0 0 0
-64
for a value of -64.
f What about 11111112 ? We have
−26 25 24 23 22 21 20
1 1 1 1 1 1 1
-64 +32 +16 +8 +4 +2 +1
for a value of -1.
This tells us that a positive number has to have a 0 in the most signifi-
cant bit position (because all the other digits with positive weight can’t
quite cancel out the negative weight of the msb). Because a positive
number has to have a 0 for the msb, and a negative number must have
a 1, this bit is called the sign bit.
f So, with 7 bits, we can express numbers between -64 and +63; equiva-
lently, between 1000000 and 0111111.

e Let’s summarize, reiterating the general rules:

f In order to represent both positive and negative numbers using two’s


complement arithmetic, we assign a negative weight to the most signifi-
cant bit. We’ll call it the sign bit

9
Cmpt 150 Binary Arithmetic February, 2012

f If the sign bit is 1, the number is negative (because the sum of all the
positively weighted bit positions cannot quite cancel the negative weight
on the most significant column). If the sign bit is 0, the number is
positive.
f With n bits, we can represent numbers from −2(n−1) to 2(n−1) − 1.

e Now, let’s come back to arithmetic, using the same two examples, 5010 −
4310 = 710 , and 5010 + 2110 = 7110 .

f Let’s look at 5010 − 4310 = 710 first. We have


−26 25 24 23 22 21 20
50 0 1 1 0 0 1 0
-43 1 0 1 0 1 0 1
7 0 0 0 0 1 1 1
Notice what happened in the top bit positions. When we added the digit
column with weight 24 , we generated a carry, and that carry propagated
through until we carried +26 into the most significant bit position. We
added it to the 1 which was there, and generated a sum of 0 and a carry
out from the sign bit. But what really happened? The carry-in of 26
cancelled the −26 already in the column, and we’re left with 0. So this
result is valid, and the carry-out is nothing to worry about.
f It’s important to stop for a moment and realise what’s happening here.
In hardware, each digit column is being added by a full adder. That full
adder neither knows nor cares about the weights we’ve assigned to digit
columns. It only knows the rules we gave it in the truth table for a full
adder.
What we’re doing is looking at the result (sum and carry-out) produced
by the full adder in the msb position, and interpreting that result in
light of our knowledge that this column is ‘special’, with weight −26 .
f Now let’s look at 5010 + 2110 = 7110 . We have
−26 25 24 23 22 21 20
50 0 1 1 0 0 1 0
+21 0 0 1 0 1 0 1
71 1 0 0 0 1 1 1
We again had a carry-in to the msb digit column, but there was no value
to cancel. The 1 that’s left there really should represent +26 , and that’s
not possible under the rules of the two’s complement representation.
The result is invalid, and we have arithmetic overflow.
f Let’s do another example: −5010 − 2110 = −7110 . We have

10
Cmpt 150 Binary Arithmetic February, 2012

−26 25 24 23 22 21 20
-50 1 0 0 1 1 1 0
-21 1 1 0 1 0 1 1
71 0 1 1 1 0 0 1
Here, we have no carry-in to the sign bit, but we do have a carry-out.
We really need to express 2 ∗ (−26 ) = −27 , and we need 8 bits to do that.
So this result, too, is invalid.
f And one final example: −43 − 21 = −64.
−26 25 24 23 22 21 20
-43 1 0 1 0 1 0 1
-21 1 1 0 1 0 1 1
-64 1 0 0 0 0 0 0
Here we seem to have a carry-in and a carry-out for the sign bit. But
don’t forget that the sign bit has a negative weight. What really hap-
pened is that we calculated (−26 ) + (−26 ) + (26 ) = −26 . This number is
perfectly legal, and we can ignore the carry-out from the msb.

e Let’s generalize. What are the cases?


We can do up a truth table:

x(msb) y(msb) ci(msb) co(msb) ovf


0 0 0 0 0
0 0 1 0 1
0 1 0 0 0
0 1 1 1 0
1 0 0 0 0
1 0 1 1 0
1 1 0 1 1
1 1 1 1 0

f From this, we could calculate overflow using x(msb), y(msb), and ci(msb),
but usually we adopt a simpler rule, easy to remember.
f Look at the columns for ci(msb) and co(msb). Where they’re the same,
we have a valid result. Where they differ, we have overflow.
f Remembering that a 2-input XOR produces a 1 when the inputs differ,
we say ovf = ci(msb) ⊕ co(msb).
In english, “if there’s a carry-in and no carry-out, or carry-out and no
carry-in, then we have arithmetic overflow.”

11
Cmpt 150 Binary Arithmetic February, 2012

e Now we can design logic to do addition and subtraction, and keep it simple.
Let’s quickly review why.

f We’ve agreed on a representation — two’s complement — which allows


us to unambiguously express both positive and negative numbers.
f We can easily calculate the two’s complement of x as ¬x + 1.
f This representation allows us to do subtraction (x − y) by doing an ad-
dition (x + (−y) = x + ¬y + 1).
f All we have to do is put the pieces together, and remember that an XOR
gate can provide a controlled complement operation.
f The circuit is
x(i+3) y(i+3) x(i+2) y(i+2) x(i+1) y(i+1) x(i) y(i)

add/sub

x y x y x y x y
FA FA FA FA
co(i+3) co ci co ci co ci co ci

Σ Σ Σ Σ

Σ(i+3) Σ(i+2) Σ(i+1) Σ(i)

ovf

This circuit will add and subtract numbers in the range 10002 to 01112
(-8 to +7, or −2(4−1) to +2(4−1) − 1).
The add/sub signal controls whether the circuit adds or subtracts. When
it’s 1 (subtract), we form the 2’s complement of y(3:0), using the XORs
for logical negation and forcing a 1 at ci(0) to implement the +1.
The overflow is calculated as the XOR of the carry-in and carry-out of
the most significant bit.

e One last small detail: sign extension.

f In two’s complement representation, only the msb (sign bit) has nega-
tive weight; all other bits have positive weight. This must hold for any
number of bits.
f Consider a four-bit value. The value will be b3 (−23 ) + b2 (22 ) + b1 (21 ) +
b0 (20 )

12
Cmpt 150 Binary Arithmetic February, 2012

Suppose that I add and subtract b3 (−24 ):

b3 (−24 ) + b3 (24 ) + b3 (−23 ) + b2 (22 ) + b1 (21 ) + b0 (20 )


b3 (−24 ) + (2)(b3 )(23 ) + b3 (−23 ) + b2 (22 ) + b1 (21 ) + b0 (20 )
b3 (−24 ) + b3 (23 ) + b2 (22 ) + b1 (21 ) + b0 (20 )

My number is again a valid two’s-complement number, unchanged in


value.
f The simple rule is ‘replicate the sign bit to fill out the required number
of bits.’

13

You might also like