0% found this document useful (0 votes)
13 views47 pages

Digital Design Lecture 3

The document discusses three methods for representing negative numbers in binary: sign magnitude, one's complement, and two's complement. It focuses on explaining the sign magnitude (SM) method. SM represents the sign of a number using the most significant bit, where 0 is positive and 1 is negative. The rest of the bits represent the magnitude. For an n-bit system, there are 2^n possible values ranging from -(2^n-1) to +(2^n-1).

Uploaded by

mostafabaiumy94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views47 pages

Digital Design Lecture 3

The document discusses three methods for representing negative numbers in binary: sign magnitude, one's complement, and two's complement. It focuses on explaining the sign magnitude (SM) method. SM represents the sign of a number using the most significant bit, where 0 is positive and 1 is negative. The rest of the bits represent the magnitude. For an n-bit system, there are 2^n possible values ranging from -(2^n-1) to +(2^n-1).

Uploaded by

mostafabaiumy94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Signed Numbers

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

+20640 = 0101 0000 1010 0000


28
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
Step (2): Flip all the bits (one’s complement)

+20640 = 0101 0000 1010 0000


one’s complement = 1010 1111 0101 1111
29
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
Step (2): Flip all the bits (one’s complement)
Step (3): two’s complement is +1

+20640 = 0101 0000 1010 0000


one’s complement = 1010 1111 0101 1111
+1
Two’ complement: -20640 = 1010 1111 0110 0000
30
Two’s Complement
Binary: convert from two’s complement:
Example: Find the decimal representation of (+ve or –ve ?)

1010 1111 0110 0000


31
Two’s Complement
Binary: convert from two’s complement:
Example: Find the decimal representation of (+ve or –ve ?)
Step (1): Flip all the bits (one’s complement)

1010 1111 0110 0000


one’s complement = 0101 0000 1001 1111
32
Two’s Complement
Binary: convert from two’s complement:
Example: Find the decimal representation of (+ve or –ve ?)
Step (1): Flip all the bits (one’s complement)
Step (2): +1

1010 1111 0110 0000


one’s complement = 0101 0000 1001 1111
+1
0101 0000 1010 0000
33
Two’s Complement
Binary: convert from two’s complement:
Example: Find the decimal representation of (+ve or –ve ?)
Step (1): Flip all the bits (one’s complement)
Step (2): +1
Step (3): Convert it into decimal

1010 1111 0110 0000


one’s complement = 0101 0000 1001 1111
+1
+20640 = 0101 0000 1010 0000
34
Two’s Complement
Binary: convert from two’s complement:
Example: Find the decimal representation of (+ve or –ve ?)
Step (1): Flip all the bits (one’s complement)
Step (2): +1
Step (3): Convert it into decimal. Then:

-20640 = 1010 1111 0110 0000


one’s complement = 0101 0000 1001 1111
+1
+20640 = 0101 0000 1010 0000
35
Two’s Complement
Sign extension: Converting the number into longer bits.
In two’s complement: Just repeat the MSB.
Example:
Decimal 4 bits 8 bits
7 0111 0000 0111
-6 1010 1111 1010

Try to convert the extended numbers and find if they are


equivalent to the original ones.
36
Two’s Complement
Unlike sign magnitude, arithmetic is simple in two’s
complement.
Example: 3-3 = 3+(-3) = ??
3= 0011
-3= 1101
Sum: (3)+(-3)= ?
37
Two’s Complement
Unlike sign magnitude, arithmetic is simple in two’s
complement.
Example: 3-3 = 3+(-3) = ??
3= 0011
-3= 1101
Sum: (3)+(-3)= 1 0000
Ignore the carry bit:
In decimal = 0 
38
Two’s Complement Decimal Binary
0 0 0 0 0
Ranges: +1 0 0 0 1
+2 0 0 1 0
Total number of values = ?? +3 0 0 1 1
+4 0 1 0 0
+5 0 1 0 1
+6 0 1 1 0
+7 0 1 1 1
??
-8 1 0 0 0
-7 1 0 0 1
-6 1 0 1 0
-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
39
Two’s Complement Decimal Binary
0 0 0 0 0
Ranges: +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
-8 1 0 0 0
Total number of values = 24 = 16 values. -7 1 0 0 1
-6 1 0 1 0
-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
40
Two’s Complement Decimal Binary
0 0 0 0 0
Ranges: +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 -8 1 0 0 0
Total number of values = 24 = 16 values. -7 1 0 0 1
-6 1 0 1 0
-5 1 0 1 1
2n-1 -4 1 1 0 0
-3 1 1 0 1
-2 1 1 1 0
-1 1 1 1 1
41
Two’s Complement Decimal Binary
0 0 0 0 0 0
Ranges: +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 -8 1 0 0 0
Total number of values = 24 = 16 values. -7 1 0 0 1
-6 1 0 1 0
-5 1 0 1 1
2 n-1
-4 1 1 0 0
-3 1 1 0 1
-2 1 1 1 0
-1 1 1 1 1
42
Two’s Complement Decimal Binary
0 0 0 0 0 0
Ranges: +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 -8 1 0 0 0
Total number of values = 24 = 16 -(2 n-1-1)
values. -7 1 0 0 1
-6 1 0 1 0
-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 1
43
Two’s Complement Decimal Binary
0 0 0 0 0 0
Ranges: +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: -2n-1  +2n-1-1 +5 0 1 0 1
+6 0 1 1 0
+(2n-1-1) +7 0 1 1 1
Example: n=4 bits -2n-1 -8 1 0 0 0
Total number of values = 24 = 16 values. -7 1 0 0 1
-6 1 0 1 0
Range of values: -2n-1  +2n-1-1 -5 1 0 1 1
Range of values: -23  +23-1 -4 1 1 0 0
-3 1 1 0 1
Range of values: -8  +7 -2 1 1 1 0
-1 -1 1 1 1 1
Decimal SM One’s Two’s
+7 0 111 0111 0111
44
Comparison +6 0 110 0110 0110
+5 0 101 0101 0101
For binary 4 bits: +4 0 100 0100 0100
+3 0 011 0011 0011
All positive numbers are +2 0 010 0010 0010
identical. +1 0 001 0001 0001
+0 0 000 0000 0000
-0
-1
-2
-3
-4
-5
-6
-7
-8
Decimal SM One’s Two’s
+7 0 111 0111 0111
45
Comparison +6 0 110 0110 0110
+5 0 101 0101 0101
For binary 4 bits: +4 0 100 0100 0100
+3 0 011 0011 0011
All positive numbers are +2 0 010 0010 0010
identical. +1 0 001 0001 0001
All negative numbers are +0 0 000 0000 0000
different. -0 1 000 1111 -
-1 1 001 1110 1111
Two’s complement has no -2 1 010 1101 1110
negative zero (feature). -3 1 011 1100 1101
Two’s complement has as -4 1 100 1011 1100
-5 1 101 1010 1011
extra negative number.
-6 1 110 1001 1010
-7 1 111 1000 1001
-8 - - 1000
Decimal SM One’s Two’s
+7 0 111 0111 0111
46
Comparison +6 0 110 0110 0110
+5 0 101 0101 0101
For binary 4 bits: +4 0 100 0100 0100
+3 0 011 0011 0011
Signe magnitude and one’s +2 0 010 0010 0010
complement have a +1 0 001 0001 0001
balanced set of positive and +0 0 000 0000 0000
negative numbers and have -0 1 000 1111 -
two representations for -1 1 001 1110 1111
zero. -2 1 010 1101 1110
-3 1 011 1100 1101
Two’s complement has an -4 1 100 1011 1100
unbalanced set of positive -5 1 101 1010 1011
and negative numbers, and -6 1 110 1001 1010
a single representations for -7 1 111 1000 1001
zero. -8 - - 1000
Decimal SM One’s Two’s
+7 0 111 0111 0111
47
Comparison +6 0 110 0110 0110
+5 0 101 0101 0101
99% of all computers use +4 0 100 0100 0100
+3 0 011 0011 0011
two’s complement notation. +2 0 010 0010 0010
+1 0 001 0001 0001
+0 0 000 0000 0000
The discussion on addition -0 1 000 1111 -
and subtraction will -1 1 001 1110 1111
therefore focus on two’s -2 1 010 1101 1110
complement arithmetic. We -3 1 011 1100 1101
will assume that implicitly. -4 1 100 1011 1100
-5 1 101 1010 1011
-6 1 110 1001 1010
-7 1 111 1000 1001
-8 - - 1000

You might also like