Download presentation
Presentation is loading. Please wait.
Published byGwendolyn Hubbard Modified over 9 years ago
1
Complement Numbers
2
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
3
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
4
Negative Numbers Representation Till now, we have only considered how unsigned (non- negative) numbers can be represented. There are three common ways of representing signed numbers (positive and negative numbers) for binary numbers: Sign-and-Magnitude 1s Complement 2s Complement
5
Negative Numbers: Sign-and-Magnitude Negative numbers are usually written by writing a minus sign in front. Example: - (12) 10, - (1100) 2 In computer memory of fixed width, this sign is usually represented by a bit: 0 for + 1 for -
6
Negative Numbers: Sign-and-Magnitude Example: an 8-bit number can have 1-bit sign and 7-bits magnitude. sign magnitude
7
Negative Numbers: Sign-and-Magnitude Largest Positive Number: 0 1111111 +(127) 10 Largest Negative Number: 1 1111111 - (127) 10 Zeroes: 0 0000000 +(0) 10 1 0000000 - (0) 10 Range: - (127) 10 to +(127) 10
8
Negative Numbers: Sign-and-Magnitude To negate a number, just invert the sign bit. Examples: - (0 0100001) sm (1 0100001) sm - (1 0000101) sm (0 0000101) sm
9
1s and 2s Complement Two other ways of representing signed numbers for binary numbers are: 1s-complement 2s-complement
10
1s Complement Given a number x which can be expressed as an n-bit binary number, its negative value can be obtained in 1s-complement representation using: - x = 2 n - x - 1 Example: With an 8-bit number 00001100, its negative value, expressed in 1s complement, is obtained as follows: -(00001100) 2 = - (12) 10 = (2 8 - 12 - 1) 10 = (243) 10 = (11110011) 1s
11
1s Complement Essential technique: invert all the bits. Examples: 1s complement of (00000001) 1s = (11111110) 1s 1s complement of (01111111) 1s = (10000000) 1s Largest Positive Number: 0 1111111 +(127) 10 Largest Negative Number: 1 0000000 -(127) 10 Zeroes: 0 0000000 1 1111111 Range: -(127) 10 to +(127) 10 The most significant bit still represents the sign: 0 = +ve; 1 = -ve.
12
1s Complement Examples (assuming 8-bit binary numbers): (14) 10 = (00001110) 2 = (00001110) 1s -(14) 10 = -(00001110) 2 = (11110001) 1s -(80) 10 = -( ? ) 2 = ( ? ) 1s
13
2s Complement Given a number x which can be expressed as an n-bit binary number, its negative number can be obtained in 2s-complement representation using: - x = 2 n - x Example: With an 8-bit number 00001100, its negative value in 2s complement is thus: -(00001100) 2 = - (12) 10 = (2 8 - 12) 10 = (244) 10 = (11110100) 2s
14
2s Complement Essential technique: invert all the bits and add 1. Examples: 2s complement of (00000001) 2s = (11111110) 1s (invert) = (11111111) 2s (add 1) 2s complement of (01111110) 2s = (10000001) 1s (invert) = (10000010) 2s (add 1)
15
2s Complement Largest Positive Number: 0 1111111 +(127) 10 Largest Negative Number: 1 0000000 -(128) 10 Zero: 0 0000000 Range: -(128) 10 to +(127) 10 The most significant bit still represents the sign: 0 = +ve; 1 = -ve.
16
2s Complement Examples (assuming 8-bit binary numbers): (14) 10 = (00001110) 2 = (00001110) 2s -(14) 10 = -(00001110) 2 = (11110010) 2s -(80) 10 = -( ? ) 2 = ( ? ) 2s
17
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
18
Comparisons of Sign-and-Magnitude and Complements Example: 4-bit signed number () Example: 4-bit signed number (positive values)
19
Comparisons of Sign-and-Magnitude and Complements Example: 4-bit signed number () Example: 4-bit signed number (negative values)
20
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
21
Complements (General) Complement numbers can help perform subtraction. With complements, subtraction can be performed by addition. Hence, A – B can be performed by A + (-B) where (-B) is represented as the complement of B. In general for Base-r number, there are: (i) Diminished Radix (or r-1’s) Complement (ii) Radix (or r’s) Complement For Base-2 number, we have seen: (i) 1s Complement (ii) 2s Complement
22
Diminished-Radix Complements Given an n-digit number, x r, its (r-1)’s complement is: (r n - 1) – x (r-1)’s complement, or 9s complement, of (22) 10 is: (10 2 - 1) - 22 = (77) 9s [This means –(22) 10 is (77) 9s ] (r-1)’s complement, or 1s complement, of (0101) 2 is: (2 4 - 1) - 0101 = (1010) 1s [This means –(0101) 2 is (1010) 1s ] Same as inverting all digits: (10 2 - 1) - 22 = 99 - 22 = (77) 9s (2 4 - 1) - 0101 = 1111 - 0101 = (1010) 1s
23
Radix Complements Given an n-digit number, x r, its r’s-complement is: r n – x r’s-complement, or 10s complement, of (22) 10 is: 10 2 - 22 = (78) 10s [This means –(22) 10 is (78) 10s ] r’s-complement, or 2s complement, of (0101) 2 is: 2 4 - 0101 = (1011) 2s [This means –(0101) 2 is (1011) 2s ] Same as inverting all digits and adding 1: (10 2 ) - 22 = (99+1) - 22 = 77 + 1 = (78) 10s (2 4 ) - 0101 = (1111+1) - 0101 = 1010 +1 = (1011) 2s
24
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
25
2s Complement Addition/Subtraction Algorithm for addition, A + B: 1.Perform binary addition on the two numbers. 2.Ignore the carry out of the MSB (most significant bit). 3.Check for overflow: Overflow occurs if the ‘carry in’ and ‘carry out’ of the MSB are different, or if result is opposite sign of A and B. Algorithm for subtraction, A – B: A – B = A + (–B) 1.Take 2s complement of B by inverting all the bits and adding 1. 2.Add the 2s complement of B to A.
26
2s Complement Addition/Subtraction Examples: 4-bit binary system Which of the above is/are overflow(s)? +3 0011 + +4 + 0100 ---- ------- +7 0111 ---- ------- -2 1110 + -6 + 1010 ---- ------- -8 11000 ---- ------- +6 0110 + -3 + 1101 ---- ------- +3 10011 ---- ------- +4 0100 + -7 + 1001 ---- ------- -3 1101 ---- -------
27
2s Complement Addition/Subtraction More examples: 4-bit binary system Which of the above is/are overflow(s)? -3 1101 + -6 + 1010 ---- ------- -9 10111 ---- ------- +5 0101 + +6 + 0110 ---- ------- +11 1011 ---- -------
28
Outline Negative Numbers Representation Sign-and-magnitude 1s Complement 2s Complement Comparison of Sign-and-Magnitude and Complements Complements Diminished-Radix Complements Radix Complements 2’s Complement Addition and Subtraction 1’s Complement Addition and Subtraction
29
1s Complement Addition/Subtraction Algorithm for addition, A + B: 1.Perform binary addition on the two numbers. 2.If there is a carry out of the MSB, add 1 to the result. 3.Check for overflow: Overflow occurs if result is opposite sign of A and B. Algorithm for subtraction, A – B: A – B = A + (–B) 1.Take 1s complement of B by inverting all the bits. 2.Add the 1s complement of B to A.
30
1s Complement Addition/Subtraction Examples: 4-bit binary system +3 0011 + +4 + 0100 ---- ------- +7 0111 ---- ------- +5 0101 + -5 + 1010 ---- ------- -0 1111 ---- ------- -2 1101 + -5 + 1010 ---- ------ -7 10111 ---- + 1 ------ 1000 -3 1100 + -7 + 1000 ---- ------- -10 10100 ---- + 1 ------- 0101
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.