Class Arithmetic

You might also like

You are on page 1of 43

Today

Binary addition
Representing negative numbers

Andrew H. Fagg: Embedded Real- 2


Time Systems: Binary Arithmetic
Binary Addition
Consider the following binary numbers:

00100110
00101011

How do we add these numbers?

Andrew H. Fagg: Embedded Real- 3


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

Andrew H. Fagg: Embedded Real- 4


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

01
And we have a carry now!

Andrew H. Fagg: Embedded Real- 5


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

001
And we have a carry again!

Andrew H. Fagg: Embedded Real- 6


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

0001
and again!

Andrew H. Fagg: Embedded Real- 7


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

10001

Andrew H. Fagg: Embedded Real- 8


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

010001
One more carry!

Andrew H. Fagg: Embedded Real- 9


Time Systems: Binary Arithmetic
Binary Addition

00100110
00101011

01010001

Andrew H. Fagg: Embedded Real- 10


Time Systems: Binary Arithmetic
Binary Addition
Behaves just like addition in decimal, but:
We carry to the next digit any time the sum
of the digits is 2 (decimal) or greater

Andrew H. Fagg: Embedded Real- 11


Time Systems: Binary Arithmetic
Negative Numbers
So far we have only talked about
representing non-negative integers
What can we add to our binary
representation that will allow this?

Andrew H. Fagg: Embedded Real- 21


Time Systems: Binary Arithmetic
Representing Negative Numbers
One possibility:
Add an extra bit that indicates the sign of
the number
We call this the sign-magnitude
representation

Andrew H. Fagg: Embedded Real- 22


Time Systems: Binary Arithmetic
Sign Magnitude Representation
+12 0 0001100

Andrew H. Fagg: Embedded Real- 23


Time Systems: Binary Arithmetic
Sign Magnitude Representation
+12 0 0001100

-12 1 0001100

Andrew H. Fagg: Embedded Real- 24


Time Systems: Binary Arithmetic
Sign Magnitude Representation
+12 0 0001100

-12 1 0001100

What is the problem with this approach?

Andrew H. Fagg: Embedded Real- 25


Time Systems: Binary Arithmetic
Sign Magnitude Representation
What is the problem with this approach?
Some of the arithmetic operators that we
have already developed do not do the right
thing

Andrew H. Fagg: Embedded Real- 26


Time Systems: Binary Arithmetic
Sign Magnitude Representation
Operator problems:
For example, we have already designed a
counter (that implements an increment
operation)

-12 1 0001100

Andrew H. Fagg: Embedded Real- 27


Time Systems: Binary Arithmetic
Sign Magnitude Representation
Operator problems:

-12 1 0001100

Increment

Andrew H. Fagg: Embedded Real- 28


Time Systems: Binary Arithmetic
Sign Magnitude Representation
Operator problems:

-12 1 0001100

Increment

1 0001101

Andrew H. Fagg: Embedded Real- 29


Time Systems: Binary Arithmetic
Sign Magnitude Representation
Operator problems:

-12 1 0001100

Increment

-13 1 0001101

!!!!
Andrew H. Fagg: Embedded Real- 30
Time Systems: Binary Arithmetic
Representing Negative Numbers
An alternative:
When taking the additive inverse of a
number, invert all of the individual bits
The leftmost bit still determines the sign of
the number

Andrew H. Fagg: Embedded Real- 31


Time Systems: Binary Arithmetic
Ones Complement Representation
12 0 0001100
Invert

-12 1 1110011

Andrew H. Fagg: Embedded Real- 32


Time Systems: Binary Arithmetic
Ones Complement Representation
12 0 0001100
Invert

-12 1 1110011

Increment

1 1110100

Andrew H. Fagg: Embedded Real- 33


Time Systems: Binary Arithmetic
Ones Complement Representation
12 0 0001100
Invert

-12 1 1110011

Increment

-11 1 1110100

Andrew H. Fagg: Embedded Real- 34


Time Systems: Binary Arithmetic
Ones Complement Representation
What problems still exist?

Andrew H. Fagg: Embedded Real- 35


Time Systems: Binary Arithmetic
Ones Complement Representation
What problems still exist?
We have two distinct representations of
zero:
0 0000000

1 0000000

Andrew H. Fagg: Embedded Real- 36


Time Systems: Binary Arithmetic
Ones Complement Representation
What problems still exist?
We cant directly add a positive and a
negative number:
12 0 0001100
+ +
-5 1 1111010

Andrew H. Fagg: Embedded Real- 37


Time Systems: Binary Arithmetic
Ones Complement Representation
12 0 0001100
+ +
-5 1 1111010

0 0000110

Andrew H. Fagg: Embedded Real- 38


Time Systems: Binary Arithmetic
Ones Complement Representation
12 0 0001100
+ +
-5 1 1111010

6 0 0000110
!!!!
Andrew H. Fagg: Embedded Real- 39
Time Systems: Binary Arithmetic
Representing Negative Numbers
An alternative:
(a little intuition first)

0 0 0000000

Decrement

Andrew H. Fagg: Embedded Real- 40


Time Systems: Binary Arithmetic
Representing Negative Numbers
An alternative:
(a little intuition first)

0 0 0000000

Decrement

1 1111111

Andrew H. Fagg: Embedded Real- 41


Time Systems: Binary Arithmetic
Representing Negative Numbers
An alternative:
(a little intuition first)

0 0 0000000

Decrement
Define this as
-1 1 1111111

Andrew H. Fagg: Embedded Real- 42


Time Systems: Binary Arithmetic
Representing Negative Numbers
A few more numbers:

3 0 0000011
2 0 0000010
1 0 0000001
0 0 0000000
-1 1 1111111
-2 1 1111110
-3 1 1111101
Andrew H. Fagg: Embedded Real- 46
Time Systems: Binary Arithmetic
Twos Complement Representation
In general, how do we take the additive
inverse of a binary number?

Andrew H. Fagg: Embedded Real- 47


Time Systems: Binary Arithmetic
Twos Complement Representation
In general, how do we take the additive
inverse of a binary number?
Invert each bit and then add 1

Andrew H. Fagg: Embedded Real- 48


Time Systems: Binary Arithmetic
Twos Complement Representation
Invert each bit and then add 1

5 0 0000101

Twos complement

-5 1 1111011

Andrew H. Fagg: Embedded Real- 49


Time Systems: Binary Arithmetic
Twos Complement Representation
Now: lets try adding a positive and a
negative number:

12 0 0001100
+ +
-5 1 1111011

Andrew H. Fagg: Embedded Real- 50


Time Systems: Binary Arithmetic
Twos Complement Representation
Now: lets try adding a positive and a
negative number:

12 0 0001100
+ +
-5 1 1111011

0 0000111
Andrew H. Fagg: Embedded Real- 51
Time Systems: Binary Arithmetic
Twos Complement Representation
Now: lets try adding a positive and a
negative number:

12 0 0001100
+ +
-5 1 1111011

7 0 0000111
Andrew H. Fagg: Embedded Real- 52
Time Systems: Binary Arithmetic
Twos Complement Representation
Twos complement is used for integer
representation in todays processors

Andrew H. Fagg: Embedded Real- 53


Time Systems: Binary Arithmetic
Twos Complement Representation
Twos complement is used for integer
representation in todays processors

One oddity: we can represent one more


negative number than we can positive
numbers

Andrew H. Fagg: Embedded Real- 54


Time Systems: Binary Arithmetic
Implementing Subtraction
How do we implement a subtraction
operator?
(e.g., A B)

Andrew H. Fagg: Embedded Real- 55


Time Systems: Binary Arithmetic
Implementing Subtraction
How do we implement a subtraction
operator?
(e.g., A B)

Take the 2s complement of B


Then add this number to A

Andrew H. Fagg: Embedded Real- 56


Time Systems: Binary Arithmetic

You might also like