°Lecture 03
°2’s Complement
1
How To Represent Signed Numbers
• Plus and minus sign used for decimal
numbers: 25 (or +25), -16, etc.
• For computers, desirable to represent
everything as bits.
• Three types of signed binary number
representations: signed magnitude, 1’s
complement, 2’s complement.
• In each case: left-most bit indicates sign:
positive (0) or negative (1).
Consider signed magnitude:
000011002 = 1210 100011002 = -1210
Sign bit Magnitude Sign bit Magnitude
2
Complement
° It has already been studied that
° subtracting one number from another is the same as making
one number negative and just adding them.
° We know how to create negative numbers in the binary
number system.
° How to perform 2’s complement process.
° How the 2’s complement process can be use to add (and
subtract) binary numbers.
° Digital electronics requires frequent addition and subtraction
of numbers. You know how to design an adder, but what about
a subtracter?
° A subtracter is not needed with the 2’s complement process.
The 2’s complement process allows you to easily convert a
positive number into its negative equivalent.
° Since subtracting one number from another is the same as
making one number negative and adding, the need for a
subtracter circuit has been eliminated. 3
3-Digit Decimal
A bicycle odometer with only
three digits is an example of a
fixed-length decimal number 999
forward (+)
system. 998
997
The problem is that without a
negative sign, you cannot tell a
+998 from a -2 (also a 998). Did 001
you ride forward for 998 miles 000
or backward for 2 miles? 999
998
Note: Car odometers do not work
this way.
002
backward (-)
001
4
Negative Decimal
How do we represent negative +499 499
pos(+)
numbers in this 3-digit decimal +498 498
number system without using a +497 497
sign?
Cut the number system in half. +001 001
Use 001 – 499 to indicate 000 000
positive numbers. -001 999
Use 500 – 999 to indicate -002 998
negative numbers.
Notice that 000 is not positive -499 501
neg(-)
or negative. -500 500
5
3-Digit Decimal (Examples)
3 003 6 006
+ 2 + 002 + (-3) + 997
5 005 3 1003
Disregard
Overflow
(-5) 995 (-2) 998
+ 2 + 002 + (-3) + 997
(-3) 997 (-5) 1995
Disregard
Overflow
It Works!
6
Complex Problem
° The previous examples demonstrate that this
process works, but how do we easily convert a
number into its negative equivalent?
° In the examples, converting the negative
numbers into the 3-digit decimal number
system was fairly easy. To convert the (-3), you
simply counted backward from 1000 (i.e., 999,
998, 997).
° This process is not as easy for large numbers
(e.g., -214 is 786). How did we determine this?
° To convert a large negative number, you can
use the 10’s Complement Process.
7
Complement
The 10’s Complement process uses base-10 (decimal) numbers.
Later, when we’re working with base-2 (binary) numbers, you will
see that the 2’s Complement process works in the same way.
First, complement all of the digits in a number.
• A digit’s complement is the number you add to the digit to make it equal
to the largest digit in the base (i.e., 9 for decimal). The complement of 0
is 9, 1 is 8, 2 is 7, etc.
Second, add 1.
• Without this step, our number system would have two zeroes (+0 & -0),
which no number system has.
8
10’s Complement Examples
Example #1
-003
Complement Digits
996
+1 Add 1
997
Example #2
-214
Complement Digits
785
+1 Add 1
786 9
One’s Complement Representation
• The one’s complement of a binary number
involves inverting all bits.
• 1’s comp of 00110011 is 11001100
• 1’s comp of 10101010 is 01010101
• For an n bit number N the 1’s complement is
(2n-1) – N.
• Called diminished radix complement by Mano
since 1’s complement for base (radix 2).
• To find negative of 1’s complement number
take the 1’s complement.
000011002 = 1210 111100112 = -1210
Sign bit Magnitude Sign bit Magnitude
10
Two’s Complement Representation
• The two’s complement of a binary number
involves inverting all bits and adding 1.
• 2’s comp of 00110011 is 11001101
• 2’s comp of 10101010 is 01010110
• For an n bit number N the 2’s complement is
(2n-1) – N + 1.
• Called radix complement by Mano since 2’s
complement for base (radix 2).
• To find negative of 2’s complement number
take the 2’s complement.
000011002 = 1210 111101002 = -1210
Sign bit Magnitude Sign bit Magnitude
11
Two’s Complement Shortcuts
° Algorithm 1 – Simply complement each bit and
then add 1 to the result.
• Finding the 2’s complement of (01100101)2 and of its 2’s
complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
--------------- ---------------
10011011 01100101
° Algorithm 2 – Starting with the least significant bit,
copy all of the bits up to and including the first 1
bit and then complementing the remaining bits.
• N =01100101
[N] =10011011
12
Finite Number Representation
° Machines that use 2’s complement arithmetic can
represent integers in the range
-2n-1 <= N <= 2n-1-1
where n is the number of bits available for
representing N. Note that 2n-1-1 = (011..11)2 and
–2n-1 = (100..00)2
o For 2’s complement more negative numbers than
positive.
o For 1’s complement two representations for zero.
o For an n bit number in base (radix) z there are zn
different unsigned values.
(0, 1, …zn-1)
13
1’s Complement Addition
° Using 1’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 1’s comp.
• (1)10 = +(0001)2 = 000012 in 1’s comp.
0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Add carry to low-order bit 0 0 1 1 0 1
Add carry 0
--------------
Final
Result
0 1 1 0 1
14
1’s Complement Subtraction
° Using 1’s complement numbers, subtracting
numbers is also easy.
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10.
- 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 1’s comp.
--------------
• (-1)10 = -(0001)2 = 111102 in 1’s comp.
1’s comp
0 1 1 0 0
Step 1: Take 1’s complement of 2nd operand Add + 1 1 1 1 0
Step 2: Add binary numbers
Step 3: Add carry to low order bit
--------------
1 0 1 0 1 0
Add carry 1
Final
--------------
Result 0 1 0 1 1
15
2’s Complement Addition
° Using 2’s complement numbers, adding numbers
is easy.
° For example, suppose we wish to add +(1100)2
and +(0001)2.
° Let’s compute (12)10 + (1)10.
• (12)10 = +(1100)2 = 011002 in 2’s comp.
• (1)10 = +(0001)2 = 000012 in 2’s comp.
0 1 1 0 0
Add + 0 0 0 0 1
Step 1: Add binary numbers --------------
Step 2: Ignore carry bit Final 0 0 1 1 0 1
Result
Ignore
16
2’s Complement Subtraction
° Using 2’s complement numbers, follow steps for
subtraction
° For example, suppose we wish to subtract
+(0001)2 from +(1100)2.
0 1 1 0 0
° Let’s compute (12)10 - (1)10.
- 0 0 0 0 1
• (12)10 = +(1100)2 = 011002 in 2’s comp.
--------------
• (-1)10 = -(0001)2 = 111112 in 2’s comp.
2’s comp
0 1 1 0 0
Step 1: Take 2’s complement of 2nd operand Add + 1 1 1 1 1
Step 2: Add binary numbers
Step 3: Ignore carry bit
--------------
Final
Result 1 0 1 0 1 1
Ignore
Carry
17
2’s Complement Subtraction: Example #2
° Let’s compute (13)10 – (5)10.
• (13)10 = +(1101)2 = (01101)2
• (-5)10 = -(0101)2 = (11011)2
° Adding these two 5-bit codes…
0 1 1 0 1
carry + 1 1 0 1 1
--------------
1 0 1 0 0 0
° Discarding the carry bit, the sign bit is seen to be
zero, indicating a correct result. Indeed,
(01000)2 = +(1000)2 = +(8)10.
18
2’s Complement Subtraction: Example #3
° Let’s compute (5)10 – (12)10.
• (-12)10 = -(1100)2 = (10100)2
• (5)10 = +(0101)2 = (00101)2
° Adding these two 5-bit codes…
0 0 1 0 1
+ 1 0 1 0 0
--------------
1 1 0 0 1
° Here, there is no carry bit and the sign bit is 1.
This indicates a negative result, which is what we
expect. (11001)2 = -(7)10.
19
1’s Complement
20
2’s Complement
• 1s complement with negative numbers shifted one
position clockwise
• Only one representation for 0
• One more negative number than positive number
• High-order bit can act as sign bit
21
Overflow
° Overflow conditions
° Add two positive numbers to get a negative number
° Add two negative numbers to get a positive number
22
Two’s Complement Number System
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0
- 1 1
0000
1111 0001 2
- 2
1110 0010
- 3 3
1101 0011
- 4 4
1100 0100
5
1011 0101
- 5
1010 0110
- 6 6
1001 1000 0111
- 7 - 8 7
23
Summary
° Binary numbers are made of binary digits (bits)
° Binary and octal number systems
° Conversion between number systems
° Addition, subtraction, and multiplication in binary
24
Summary
° Binary numbers can also be represented in octal and
hexadecimal
° Easy to convert between binary, octal, and hexadecimal
° Signed numbers represented in signed magnitude, 1’s
complement, and 2’s complement
° 2’s complement most important (only 1 representation
for zero).
° Important to understand treatment of sign bit for 1’s
and 2’s complement.
25