Binary Number Arithmetic

Download Report

Transcript Binary Number Arithmetic

Binary Arithmetic
• Adding Binary numbers
• Overflow conditions
• How does it all work?
• AND, OR and NOT
Decimal Addition
• You are probably already familiar with adding
decimal numbers
7
+2
9
5
+1
6
2
+3
5
• You also know that when the addition of two
numbers exceeds the base, a value is “carried” over
to the next column
Carry the “1”
1
7
+ 5
12
Binary Addition
• Adding binary numbers employs the same
procedures as adding decimal numbers:
00
01
00
+00 +00 +01
00
01
01
• As with decimal addition, when the addition of two
numbers exceeds the base value, the value is
Carry the “1”
“carried” over to the next column
1
01
+01
10
Binary Addition Examples
01001100
01101010
10110110
11001100
00110011
11111111
01111111
00000001
10000000
• What happens when we have the following case?
1
11
+11
???
Carry the “1”
More Binary Addition Examples
01111111
01000001
11000000
10001100
00111011
11000111
01111111
01001001
11001000
• What happens when we get the following case?
(note: assume that we are limited to 8 bits)
11111111
00000001
????????
Overflow
• In the following case, we have a condition called
“overflow”. The result may be somewhat unexpected
1
Carry the “1”
11111111
00000001
00000000
• When adding two numbers, it is possible that the
result will not fit within the space we have provided.
The addition completes, but part of the value is lost.
• Luckily, overflow is often considered to be an error
condition, so the program “knows” that this has
happened and can take appropriate action.
How does it all work?
• In the first week of lectures, we discussed vacuum
tubes, relays, and transistors
• As we went through the discussion, I asked, “How
could you add two numbers using switches?”
• I didn’t expect an answer, but I did show how switches
could be set up to implement the AND function and the
OR function.
AND, OR and NOT
• You may recall the “truth” tables for the AND and OR
functions:
A
0
B 0 0
1 0
A
1
0
1
0
B 0 0
1 1
AND
OR
A
1
1
1
0
1
1
0
NOT (~)
• The AND, OR and NOT functions are the basic
functions for “Boolean” Algebra
Binary Arithmetic/AND and OR
• All binary arithmetic can be represented using
boolean algebra
• Each 1-bit adder has 3 inputs: A, B and CarryIn
• Each 1-bit adder has 2 output: C and CarryOut
CIN
A
B
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
C
COUT
0 1 1 0 1 0 0 1
0 0 0 1 0 1 1 1
Boolean Equations for Addition
CIN
A
B
0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
C
COUT
0 1 1 0 1 0 0 1
0 0 0 1 0 1 1 1
C = (~A and B and ~CIN) or (A and ~B and ~CIN) or
(~A and ~B and CIN) or (A and B and CIN)
COUT= (A and B) or (A and CIN) or (B and CIN)