Electrical Engineering 1
Lecture 2: Signed Binary Numbers
Overview
· The type of binary introduced in week 2 can only be used for
positive binary numbers
· In this lecture we extend binary to accommodate negative
numbers
Negative numbers
· Suppose we have a 4-bit data item
· There are 16 possibilities for what bit pattern it can hold
· It’s up to us to decide what meaning we want these 16
bit patterns to have in our designs
· One possibility is the unsigned “pure binary” meaning:
Meaning Data item Meaning Data item
0000 1000
0001 1001
0010 1010
0011 1011
0100 1100
0101 1101
0110 1110
0111 1111
Negative numbers
· Suppose we have a 4-bit data item
· There are 16 possibilities for what bit pattern it can hold
· It’s up to us to decide what meaning we want these 16
bit patterns to have in our designs
· One possibility is the unsigned “pure binary” meaning:
Meaning Data item Meaning Data item
0 0000 8 1000
1 0001 9 1001
2 0010 10 1010
3 0011 11 1011
4 0100 12 1100
5 0101 13 1101
6 0110 14 1110
7 0111 15 1111
Negative numbers
· Now we’ll look at other choices that give new possibilities
· Want a way to handle numbers that can be positive or
negative
Meaning Data item Meaning Data item
0000 1000
0001 1001
0010 1010
0011 1011
0100 1100
0101 1101
0110 1110
0111 1111
Signed Magnitude
· Use leftmost bit as a sign
Number Representation
· 6 is 110 -7 1111
· +6 is 0110 -6
-5
1110
1101
Sign bit
· -6 is 1110 -4 1100
-3 1011
-2 1010
16 different bit patterns -1 1001
Only 15 different numbers -0 1000
0 0000
can be represented because 1 0001
+0 and -0 are different 2 0010
numbers (that’s awkward) 3 0011
4 0100
5 0101
6 0110
7 0111
Signed Magnitude
· Difficult to construct adder circuit:
· Check sign bit
(+5) + (+2) = +(5+2) = +7
If same (-5) + (-2) = -(5+2) = -7
Add numbers and give result same sign bit
If different
(+5) + (-2) = +(5-2) = +3
Find which is larger and smaller (-5) + (+2) = -(5-2) = -3
Subtract smaller from larger
Give result sign of larger
· Gives very complicated circuit
Counters with Fixed Number of Digits
· Cars have odometers
Count number of miles (or km) driven
· Older cars had only 5 digit odometers:
After 99,999 miles
Drive 1 mile further and odometer says 00,000
· If you were buying a 2nd hand car and odometer said
20,000
That could be a true reading,
or it could be 120,000 miles
2s Complement
· Basic idea:
· 4-bit counter follows sequence
0000
0001
-1 means the number that gives
…
zero when we add one to it
1100
1101
1110 This is the number that gives
1111 zero when we add one
0000 Use this to represent -1
…
2s Complement
· Basic idea:
· 4-bit counter follows sequence
0000
0001
-2 means the number that gives
…
zero when we add 2 to it
1100
1101 This is the number that gives
1110 zero when we add 2
1111 Use this to represent -2
0000
2s complement
· 2s complement gives most significant bit negative weight
· 4-bit number 1010 is interpreted as
1×-8 + 0×4 + 1×2 + 0×1 = (-6)10
· 6-bit number 111010 is interpreted as
1×-32 + 1×16 + 1×8 + 0×4 + 1×2 + 0×1 = (-6)10
· 6-bit number 001010 is interpreted as
0×-32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = (10)10
Computing 2s Complement
Number Representation
· To negate number: -8 1000
-7 1001
Flip all bits and add 1 -6 1010
· Example: -5
-4
1011
1100
Represent -6 as 4-bit number -3 1101
(+6)10 is (0110)2 -2 1110
-1 1111
Flip all bits: 1001 0 0000
1 0001
Add 1 gives 1010 2 0010
(-6)10 is (1010)2 3 0011
4 0100
5 0101
6 0110
7 0111
Interpreting 2s Complement
· Unsigned binary · 2s complement
· All digits have positive weight · Msb has negative weight
1010 = 1010
= 123 + 022 + 121 + 020 = 1-23 + 022 + 121 + 020
= 18 + 04 + 12 + 01 = 1-8 + 04 + 12 + 01
=8+2 = -8 + 2
= 10 = -6
Signed
Unsigned 2s complement
2s comp addition 0
1
0000
0001
-8
-7
1000
1001
2 0010 -6 1010
· Unsigned binary 3 0011 -5 1011
4 0100 -4 1100
1100 (denary 12)
+ 0010 (denary 2) 5 0101 -3 1101
= 1110 (denary 14) 6 0110 -2 1110
7 0111 -1 1111
8 1000 0 0000
· 2s complement 9 1001 1 0001
1100 (denary -4) 10 1010 2 0010
+ 0010 (denary 2) 11 1011 3 0011
= 1110 (denary -2) OK 12 1100 4 0100
13
An adder that works for unsigned binary 1101 5 0101
14 1110
needs no modification to work for 2s complement 6 0110
15 1111 7 0111
Overflow in 2s Complement
· Overflow occurs when result cannot be represented in
available bits
· We’ll look at some 4-bit examples
· Unsigned (“pure”) binary
Number range is 0 to 15
Overflow can be detected when msb’s C
OUT=1
· 2s complement
Number range is -8 to +7
Overflow can be detected when
Input numbers both positive but output is negative
Input numbers both negative but output is positive
Overflow in 2s Complement
· Unsigned (“pure”) 4-bit binary
Number range is 0 to 15
Overflow can be detected when msb’s COUT=1
· 2s complement 4-bit
Number range is -8 to +7
Overflow when result has opposite sign to both addends
Pure binary 2s complement
1 1 1 1 Denary 15 1 1 1 1 Denary -1
+ 0 0 1 0 Denary 2 + 0 0 1 0 Denary 2
0 0 0 1 Denary 1 0 0 0 1 Denary +1
1 11 0 1 11 0
Overflow (COUT3=1) No problem
Input signs are different
Overflow in 2s Complement
· Unsigned (“pure”) 4-bit binary
Number range is 0 to 15
Overflow can be detected when msb’s COUT=1
· 2s complement 4-bit
Number range is -8 to +7
Overflow when result has opposite sign to both addends
Pure binary 2s complement
0 1 1 1 Denary 7 0 1 1 1 Denary 7
+ 0 0 1 0 Denary 2 + 0 0 1 0 Denary 2
1 0 0 1 Denary 9 1 0 0 1 Denary -7
0 11 0 0 11 0
No problem (COUT3=0) Overflow
Input signs are same
Output sign is opposite
Overflow in 2s Complement
· Unsigned (“pure”) 4-bit binary
Number range is 0 to 15
Overflow can be detected when msb’s COUT=1
· 2s complement 4-bit
Number range is -8 to +7
Overflow when result is opposite sign to both addends
Pure binary 2s complement
1 0 0 1 Denary 9 1 0 0 1 Denary -7
+ 1 1 1 0 Denary 14 + 1 1 1 0 Denary -2
0 1 1 1 Denary 7 0 1 1 1 Denary 7
1 00 0 1 00 0
Overflow (COUT3=1) Overflow
Input signs are same
Output sign is opposite
Is Overflow Common?
· In lectures we deal with small numbers (e.g. 4-bit)
because they are easy to think about and illustrate
· That means we often see overflow
· In real world designs, numbers are more likely to be 16-
bit, 24-bit, 32-bit
· Overflow is normally uncommon
Adder with Overflow Detection
· Adder just adds bit patterns
· It doesn’t know if you want to interpret result as unsigned
or as 2s complement
· But overflow conditions are different – what to do?
· Adder computes 2 status flags
B3 A 3 B2 A 2 B1 A 1 B0 A 0
C
V
Q3 Q2 Q1 Q0
Adder with Overflow Detection
· Status flags
· C flag: msb’s COUT=1
if we interpret as unsigned then overflow occurred
· V flag:
if we interpret as 2s complement then overflow occurred
B3 A 3 B2 A 2 B1 A 1 B0 A 0
C
V
Q3 Q2 Q1 Q0
Summary
· We need to extend binary to represent negative numbers
· Standard scheme is 2s complement
· Addition with fixed number of bits can overflow:
Answer can’t be represented in available number of bits
· Status flags can be added to adder to indicate when a
problem has occurred