Digital Design Lecture 3
Digital Design Lecture 3
1
2
Negative numbers
How to represent negative numbers in binary?
There are three main methods for representing negative
numbers in microprocessors:
1) Sign magnitude.
2) One’s complement.
3) Two’s complement.
3
Sign Magnitude (SM)
Reserve the Most Significant Bit (MSB) for sign:
0: Positive. 1: Negative.
The other bits are for the magnitude.
Examples: For 16-bit numbers
Decimal Binary
+160 0 000 0000 1010 0000
−160 ??
4
Sign Magnitude (SM)
Reserve the Most Significant Bit (MSB) for sign:
0: Positive. 1: Negative.
The other bits are for the magnitude.
Examples: For 16-bit numbers
Decimal Binary
+160 0 000 0000 1010 0000
−160 1 000 0000 1010 0000
5
Sign Magnitude (SM)
Reserve the Most Significant Bit (MSB) for sign:
0: Positive. 1: Negative.
The other bits are for the magnitude.
Examples: For 16-bit numbers
Decimal Binary
+160 0 000 0000 1010 0000
−160 1 000 0000 1010 0000
−20640 1 101 0000 1010 0000
+20640 0 101 0000 1010 0000
6
Sign Magnitude (SM)
How to find the magnitude of a given number in sign-
magnitude form?
AND the number with 0111…1111 (mask).
Example:
Given: 1 101000010100000
Mask: (AND) 0 111111111111111
Magnitude: 0101000010100000
In decimal, magnitude = 5340810
In decimal, number = -5340810
7
Sign Magnitude (SM) Decimal Binary
?? 0 0 0 0
Range (binary system): ?? 0 0 0 1
?? 0 0 1 0
Total number of values = ?? ?? 0 0 1 1
?? 0 1 0 0
?? 0 1 0 1
?? 0 1 1 0
?? 0 1 1 1
??
?? 1 0 0 0
?? 1 0 0 1
?? 1 0 1 0
?? 1 0 1 1
?? 1 1 0 0
?? 1 1 0 1
?? 1 1 1 0
?? 1 1 1 1
8
Sign Magnitude (SM) Decimal Binary
?? 0 0 0 0
Range (binary system): ?? 0 0 0 1
?? 0 0 1 0
Total number of values = 2n ?? 0 0 1 1
?? 0 1 0 0
Range of values: ?? ?? 0 1 0 1
?? 0 1 1 0
?? 0 1 1 1
Example: n=4 bits 2n
?? 1 0 0 0
Total number of values = 24 = 16 values. ?? 1 0 0 1
?? 1 0 1 0
?? 1 0 1 1
?? 1 1 0 0
?? 1 1 0 1
?? 1 1 1 0
?? 1 1 1 1
9
Sign Magnitude (SM) Decimal Binary
+0 0 0 0 0
Range (binary system): +1 0 0 0 1
+2 0 0 1 0
Total number of values = 2n +3 0 0 1 1
+4 0 1 0 0
Range of values: ?? +5 0 1 0 1
+6 0 1 1 0
+7 0 1 1 1
Example: n=4 bits 2n
-0 1 0 0 0
Total number of values = 24 = 16 values. -1 1 0 0 1
-2 1 0 1 0
-3 1 0 1 1
-4 1 1 0 0
-5 1 1 0 1
-6 1 1 1 0
-7 1 1 1 1
10
Sign Magnitude (SM) Decimal Binary
+0 0 0 0 0
Range (binary system): +1 0 0 0 1
+2 0 0 1 0
Total number of values = 2n 2n/2
+3 0 0 1 1
+4 0 1 0 0
Range of values: ?? +5 0 1 0 1
+6 0 1 1 0
+7 0 1 1 1
Example: n=4 bits -0 1 0 0 0
Total number of values = 24 = 16 values. -1 1 0 0 1
-2 1 0 1 0
-3 1 0 1 1
2n/2 -4 1 1 0 0
-5 1 1 0 1
-6 1 1 1 0
-7 1 1 1 1
11
Sign Magnitude (SM) Decimal Binary
+0 0 0 0 0
Range (binary system): +1 0 0 0 1
+2 0 0 1 0
Total number of values = 2n 2n-1
+3 0 0 1 1
+4 0 1 0 0
Range of values: ?? +5 0 1 0 1
+6 0 1 1 0
+7 0 1 1 1
Example: n=4 bits -0 1 0 0 0
Total number of values = 24 = 16 values. -1 1 0 0 1
-2 1 0 1 0
-3 1 0 1 1
2n-1 -4 1 1 0 0
-5 1 1 0 1
-6 1 1 1 0
-7 1 1 1 1
12
Sign Magnitude (SM) Decimal Binary
0 +0 0 0 0 0
Range (binary system): +1 0 0 0 1
+2 0 0 1 0
Total number of values = 2n +3 0 0 1 1
+4 0 1 0 0
Range of values: ?? +5 0 1 0 1
+6 0 1 1 0
+(2n-1-1) +7 0 1 1 1
Example: n=4 bits -0 1 0 0 0
0
Total number of values = 24 = 16 values. -1 1 0 0 1
-2 1 0 1 0
-3 1 0 1 1
-4 1 1 0 0
-5 1 1 0 1
-6 1 1 1 0
-(2n-1-1) -7 1 1 1 1
13
Sign Magnitude (SM) Decimal Binary
0 +0 0 0 0 0
Range (binary system): +1 0 0 0 1
+2 0 0 1 0
Total number of values = 2n +3 0 0 1 1
+4 0 1 0 0
Range of values: -(2 -1) +(2 -1)
n-1 n-1
+5 0 1 0 1
+6 0 1 1 0
+(2n-1-1) +7 0 1 1 1
Example: n=4 bits -0 1 0 0 0
0
Total number of values = 24 = 16 values. -1 1 0 0 1
-2 1 0 1 0
Range of values: -(2 -1) +(2 -1)
3 3
-3 1 0 1 1
Range of values: -7 +7 -4 1 1 0 0
-5 1 1 0 1
-6 1 1 1 0
-(2n-1-1) -7 1 1 1 1
14
Sign Magnitude (SM)
Sign extension:
If you gave a number in 3-bits and you need to store it in 8
bits. You will need sign extension.
If you are adding two numbers, they must be of the same
length. If not, you will need sign extension.
Example: Given a binary number 1011 represented by SM. It
is required to store it in 8 bits.
Answer: ??
15
Sign Magnitude (SM)
Sign extension:
If you gave a number in 3-bits and you need to store it in 8
bits. You will need sign extension.
If you are adding two numbers, they must be of the same
length. If not, you will need sign extension.
Example: Given a binary number 1011 represented by SM. It
is required to store it in 8 bits.
Answer: keep the MSB (sign bit) and pad zeros before it.
1011 = 10000011
16
Sign Magnitude (SM)
Sign extension:
If you gave a number in 3-bits and you need to store it in 8
bits. You will need sign extension.
If you are adding two numbers, they must be of the same
length. If not, you will need sign extension.
Example: Given a binary number 0011 represented by SM. It
is required to store it in 8 bits.
Answer: keep the MSB (sign bit) and pad zeros before it.
0011 = 00000011
Pad zeros to the right of the sign bit.
17
Sign Magnitude (SM)
However, it is hard to use sign magnitude representation for
arithmetic operations.
Example: In binary (3) + (-3) = ?
3= 0000 0011
-3= ??
18
Sign Magnitude (SM)
However, it is hard to use sign magnitude representation for
arithmetic operations.
Example: In binary (3) + (-3) = ?
3= 0000 0011
-3= 1000 0011
Sum: (3)+(-3)= 1000 0110
In decimal = ??
19
Sign Magnitude (SM)
However, it is hard to use sign magnitude representation for
arithmetic operations.
Example: In binary (3) + (-3) = ?
3= 0000 0011
-3= 1000 0011
Sum: (3)+(-3)= 1000 0110
In decimal = -6 (error)
Solution: If the signs are the same, we add the magnitudes
as unsigned numbers. If the signs differ, we subtract the
smaller from the larger, and keep the sign of the larger.
20
One’s Complement Decimal Binary
0 0 0 0
0 0 0 1
Another method for representing signed
0 0 1 0
numbers. 0 0 1 1
0 1 0 0
0 1 0 1
Assume 4-bits number. 0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
21
One’s Complement Decimal Binary
+0 0 0 0 0
+1 0 0 0 1
Another method for representing signed
+2 0 0 1 0
numbers. +3 0 0 1 1
+4 0 1 0 0
+5 0 1 0 1
Assume 4-bits number. +6 0 1 1 0
+7 0 1 1 1
Divide the range into positive and negative 1 0 0 0
numbers. 1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
22
One’s Complement Decimal Binary
+0 0 0 0 0
+1 0 0 0 1
Another method for representing signed
+2 0 0 1 0
numbers. +3 0 0 1 1
+4 0 1 0 0
+5 0 1 0 1
Assume 4-bits number. +6 0 1 1 0
+7 0 1 1 1
Divide the range into positive and negative -7 1 0 0 0
numbers. -6 1 0 0 1
-5 1 0 1 0
We get the negative number by flipping each -4 1 0 1 1
bit. -3 1 1 0 0
-2 1 1 0 1
Note that the positive part here is the same -1 1 1 1 0
positive part as SM method. -0 1 1 1 1
23
One’s Complement Decimal Binary
+0 0 0 0 0
+1 0 0 0 1
Negation method:
+2 0 0 1 0
Flip each bit. +3 0 0 1 1
+4 0 1 0 0
More generally, replace each bit X by 1-X. +5 0 1 0 1
So, it is one’s complement. +6 0 1 1 0
+7 0 1 1 1
-7 1 0 0 0
-6 1 0 0 1
Note that in binary system the one is 2-1 = R-1.
-5 1 0 1 0
-4 1 0 1 1
-3 1 1 0 0
-2 1 1 0 1
-1 1 1 1 0
-0 1 1 1 1
24
Two’s Complement Decimal Binary
0 0 0 0 0
+1 0 0 0 1
Assume 4-bits number.
+2 0 0 1 0
Divide the range into positive and negative +3 0 0 1 1
+4 0 1 0 0
numbers.
+5 0 1 0 1
+6 0 1 1 0
+7 0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
25
Two’s Complement Decimal Binary
0 0 0 0 0
+1 0 0 0 1
Assume 4-bits number.
+2 0 0 1 0
Divide the range into positive and negative +3 0 0 1 1
+4 0 1 0 0
numbers.
+5 0 1 0 1
Then, if X is a positive number: +6 0 1 1 0
+7 0 1 1 1
-X = 2n-X -8 1 0 0 0
Example: X = +3 and n=4 -7 1 0 0 1
-6 1 0 1 0
-3 = ?? -5 1 0 1 1
-4 1 1 0 0
-3 1 1 0 1
-2 1 1 1 0
-1 1 1 1 1
26
Two’s Complement Decimal Binary
0 0 0 0 0
+1 0 0 0 1
Assume 4-bits number.
+2 0 0 1 0
Divide the range into positive and negative +3 0 0 1 1
+4 0 1 0 0
numbers.
+5 0 1 0 1
Then, if X is a positive number: +6 0 1 1 0
+7 0 1 1 1
-X = 2n-X -8 1 0 0 0
Example: X = +3 and n=4 -7 1 0 0 1
-6 1 0 1 0
-3 = 24-3 = 13 = 1101 -5 1 0 1 1
-4 1 1 0 0
-3 1 1 0 1
Note that the MSB is always 1. -2 1 1 1 0
-1 1 1 1 1
27
Two’s Complement
Binary: convert to two’s complement:
Example: Find the binary representation of −20640
Step (1): Find the binary representation of positive 20640