L3_2 Two’s Complement
EECS 370 – Introduction to Computer Organization – Fall 2020
EECS 370 – Introduction to Computer Organization – © Bill Arthur 1 The material in this presentation cannot be copied in any form without written permission
Learning Objectives
• Represent signed and unsigned numbers in binary (base 2)
• Negate positive and negative signed values
• Complete arithmetic operations (addition and subtraction) by hand using signed and unsigned binary numbers
EECS 370 – Introduction to Computer Organization
2
Binary Review
• Before starting this video, get comfortable with representing numbers in binary
• Resources:
• Video reviews (EECS 370 website):
https://www.eecs.umich.edu/courses/eecs370/eecs370.f20/video_reviews/
• Resource documents (EECS 370 website): https://www.eecs.umich.edu/courses/eecs370/eecs370.f20/resources/
• Lecture 2 video on Binary numbers: https://drive.google.com/drive/folders/1RDqMynHaAMFW6hRLvky9XJZR2dh BD-bl or Media gallery on Canvas https://umich.instructure.com/courses/394380/external_tools/6329
EECS 370 – Introduction to Computer Organization
3
Binary Addition
• We can already represent non-negative numbers in binary
6 (base 10) = 22 (4) + 21 (2) = 110 (base 2)
• We can do arithmetic with binary numbers
3 + 2 = 5 (base 10)
3 + 5 = 8 (base 10)
EECS 370 – Introduction to Computer Organization
4
Binary Addition
• We can already represent non-negative numbers in binary
6 (base 10) = 22 (4) + 21 (2) = 110 (base 2)
• We can do arithmetic with binary numbers
3 + 2 = 5 (base 10) 3 + 5 = 8 (base 10)
EECS 370 – Introduction to Computer Organization
5
What about Negative Numbers?
• Thoughts: add another bit for sign, use one of the existing bits for sign
EECS 370 – Introduction to Computer Organization
6
What about Negative Numbers?
• Design space preferences:
• Representation of positive and negative values
• Representation of signed and unsigned values
• Single way to represent 0
• Equal magnitude of positive and negative values (roughly) • Simple (not complex) to detect sign (positive or negative) • Simple negation of a number
• Simple storage for signed and unsigned
• Simple, non-redundant hardware for operations
• E.g., one hardware addition unit for signed and unsigned numbers
EECS 370 – Introduction to Computer Organization
7
What about Negative Numbers?
• Design space preferences:
• Representation of positive and negative values
• Representation of signed and unsigned values
• Single way to represent 0
• Equal magnitude of positive and negative values (roughly) • Simple (not complex) to detect sign (positive or negative) • Simple negation of a number
• Simple storage for signed and unsigned
• Simple, non-redundant hardware for operations
• E.g., one hardware addition unit for signed and unsigned numbers
• Thought: use existing bit of binary number for signed values EECS 370 – Introduction to Computer Organization
Two’s Complement
8
Unsigned Binary Representation
• 1011 in binary is 13 in decimal
1 1 0 1 = 8 + 4 + 1 = 13
23 22 21 20
EECS 370 – Introduction to Computer Organization
9
Two’s Complement Binary Representation
• 1011 in binary is 13 in decimal
1 1 0 1 = 8 + 4 + 1 = 13
23 22 21 20
• Two’s complement numbers are very similar to unsigned binary EXCEPT the first (most significant) digit is negative in two’s complement
1 1 0 1 =-8+4+1=-3
-(23)22 21 20 EECS 370 – Introduction to Computer Organization
10
Two’s Complement – Exercise 1
What is 1010 (binary)
1. Decimal unsigned value?
2. Decimal signed (two’s complement) value?
unsigned
1010
23 22 21 20
Signed – 2’s complement
1010
-(23)22 21 20
EECS 370 – Introduction to Computer Organization
11
Two’s Complement – Exercise 1
What is 1010 (binary)
1. Decimal unsigned value?
2. Decimal signed (two’s complement) value?
unsigned
1010
23 22 21 20
Signed – 2’s complement
1010
-(23)22 21 20
EECS 370 – Introduction to Computer Organization
12
Two’s Complement – Exercise 1
What is 1010 (binary)
1. Decimal unsigned value?
2. Decimal signed (two’s complement) value?
unsigned
1010
23 22 21 20
8 + 2 = 10
Signed – 2’s complement
1010
-(23)22 21 20 -8 + 2 = -6
EECS 370 – Introduction to Computer Organization
13
Two’s Complement Range
• What is the range of representation of a 4-bit 2’s complement number?
• [ -8, 7 ]
• What is the range of representation of an n-bit 2’s complement
number?
• [ -2(n-1) , 2(n-1) -1 ]
EECS 370 – Introduction to Computer Organization
14
Negating Two’s Complement
• Useful trick: You can negate a 2’s complement number by inverting all the bits and adding 1.
5 (decimal) in binary is
Negate (invert) all bits
Add 1
0 10 1
1 01 0
1 0 1 0 +0 0 0 1 1011
-(23) 22 21 20
-8 + 0 + 2 + 1 = -5
EECS 370 – Introduction to Computer Organization
15
Two’s Complement – Exercise 2
How would you represent -3 (decimal) in 2’s complement binary using 4 bits?
EECS 370 – Introduction to Computer Organization
16
Two’s Complement – Exercise 2
How would you represent -3 (decimal) in 2’s complement binary using 4 bits?
1. Convert 3 (decimal) to binary 2. Negate binary
1. Invert all bits 2. Add one
EECS 370 – Introduction to Computer Organization
17
Two’s Complement – Exercise 2
How would you represent -3 (decimal) in 2’s complement binary using 4 bits?
1. Convert 3 (decimal) to binary 2. Negate binary
1. Invert all bits 2. Add one
EECS 370 – Introduction to Computer Organization
18
Two’s Complement – Exercise 2
How would you represent -3 (decimal) in 2’s complement binary using 4 bits?
1. Convert 3 (decimal) to binary 2. Negate binary
1. Invert all bits 2. Add one
Signed – 2’s complement
1101
-23 22 21 20
-8 + 4 + 0 + 1 = -3
1. Convert 3 to binary 1. 3 -> 0011
2. Convert to 2’s complement 1. 0011 -> 1100
2. 1100 + 1 = 1101
EECS 370 – Introduction to Computer Organization
19
Sign Extension
• With two’s compliment, it matters how many bits are used! 5 (decimal) in binary (4 bits) is 0101
5 (decimal) in binary (8 bits) is 0000 0101
-5 (decimal) in binary (4 bits) is 1011
-5 (decimal) in binary (8 bits) is 1111 1011
NOT 0000 1011
need to extend the most significant (sign) bit LC-2K: programmer (you) need to do this!
EECS 370 – Introduction to Computer Organization
20
Two’s Complement Arithmetic
7 – 6 = 7 + (-6) = 1 6 – 7 = 6 + (-7) = -1
EECS 370 – Introduction to Computer Organization
21
Two’s Complement Arithmetic
7 – 6 = 7 + (-6) = 1 6 – 7 = 6 + (-7) = -1
EECS 370 – Introduction to Computer Organization
22
Logistics
• This is the second of 3 videos for lecture 3 • L3_1 – ISAs – Instructions and Memory
• L2_2 – Two’s Complement • L2_3 – LC-2K ISA
• There are two worksheets for lecture 3
1. Addressing and 2’s complement
2. LC-2K program encoding
• When ready, move on to L3_3
EECS 370 – Introduction to Computer Organization
23