Complement Number Systems A complement number system is used to represent positive and negative integers A complement number system is based on a fixed length representation of numbers Pretend that integers are represented by two decimal digits: (00) 10c through (99) 10c This is a two digit, tens complement number system If (a) 10c + (b) 10c = 100, then the 2-digit, tens complement numbers (a) 10c and (b) 10c are negatives of one another In a 2-digit tens complement system, the overflow carry digit 1 is discarded Complement Number Systems Consider the examples: 1 1 0 Carry 3 4 6 6 1 0 0 1 1 0 Carry 4 9 5 1 1 0 0 1 1 0 Carry 1 9 9 1 0 0 By convention 34, 49, and 1 are all positive and represent their working values And, 66, 51, and 99 represent 34, 49, and 1 4-Bit Twos Complement Number System Consider a 4-bit twos complement number system of numbers That is, integers are represented by four bits: (0000) 2c through (1111) 2c 1
Complement Number Systems You can draw a graph that shows the relationship between complement numbers and their working decimal definitions Here s the graph of the 4-digit tens complement system decimal values 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A B C D 14 E 15 F 0 1 2 3 4 5 6 7 complement numbers In this example, a working decimal value a is represented by the complement number a where { a if 0 a < 8 a = a + 16 if a < 0 Why Use Complement Number Systems? A complement number system make addition and subtraction easy to implement in hardware In fact, there no subtraction, just addition on integers (positive and negative whole numbers) Using the common signed-magnitude notation, computing a b when a b and when a < b requires different logic A good exercise is to write the logic for computing a b under these two conditions Tens Complement Notation Here are two examples of negatives in the tens complement numbers system 1 Let a = (67) 10c and b = (33) 10c be 2 digit, tens complement numbers Since a + b = 67 + 33 = 100, (67) 10c and (33) 10c are negatives of each other The convention is (33) 10c = 33 and (67) 10c = 33 2 Let a = (048) 10c and b = (952) 10c be 3 digit, tens complement numbers Since a + b = 48 + 952 = 1000, (048) 10c and (952) 10c are negatives of each other The convention is (048) 10c = 48 and (952) 10c = 48 2
Tens Complement Notation Using two digits: (00) 10 through (49) 10c are positive and represent their normal values While, (50) 10c through (99) 10c are negative and represent values 50 through 1 Using three digits: (000) 10 through (499) 10c are positive and have their normal values While, (500) 10c through (999) 10c are negative and have values 500 through 1 Tens Complement Notation The length of tens complement numbers can be padded Positive numbers can be padded with 0 s on the left This is because 34 = (34) 10c = (034) 10c = (0034) 10c = (00034) 10c 34 66 034 966 0034 9966 00034 99966 100 1000 10000 100000 Likewise, negative numbers can be padded with 9 s on the left 21 = (79) 10c = (974) 10c = (9979) 10c = (99979) 10c Twos Complement Notation Twos complement notation is interesting because computers most often store integers in twos complement notation The basic idea is identical to all notations The twos complement numbers (a) 2c and (b) 2c are negatives of one another if they sum to 0 For instance, Carries 1 1 1 1 1 1 0 0 (a) 2c 1 0 0 1 1 1 0 0 (b) 2c 0 1 1 0 0 1 0 0 Sum 1 0 0 0 0 0 0 0 0 In an 8-bit word, the last (ninth) overflow carry bit is discarded 3
Simple Test of Positive and Negative Twos Complement Numbers The most significant (leftmost) bit indicates if a twos complement number is positive or negative Small numbers, those with a leading 0, are positive and represent their normal values Large numbers, those with a leading 1, are negative and represent a negatively shifted value The amount of the negative shift depend on the size of the representation How to Negate a Twos Complement Number Algorithm 1 (Negating a Twos Complement Number) Given a twos complement number (a) 2c 1 Copy (a) 2c s bits from right-to-left up to and including the first 1 2 Flip the remaining bits in (a) 2c Examples of Negating Twos Complement Numbers Consider the examples: 1 The negative of (1001 0000) 2c is (0111 0000) 2c The least significant bits 1 0000 are copied up to and including the first 1 The remaining bits 100 are flipped to get 011 2 The negative of (1001 0100) 2c is (0110 1100) 2c The least significant bits 100 are copied The remaining bits 1001 0 are flipped to get 0110 1 Converting a Twos Complement Number to Decimal Algorithm 2 (Conversion of Twos Complement to Decimal) Let (a) 2c be a twos complement number 1 If (a) 2c is not negative (if the most significant [leftmost] bit is 0) use Horner s rule to convert (a) 2c 2 If (a) 2c is not positive (if the most significant [leftmost] bit is 1) either: (a) use Horner s rule to convert (a) 2c and subtract 2 a from the result, or (b) Negate (a) 2c using the Negation Algorithm, use Horner s rule on the negative, and negate the result 4
Example: Twos Complement Number to Decimal Let a = (0011 1100) 2c be a twos complement number Since a is positive (leftmost bit is 0) Simply use Horner s rule to convert a to decimal Horner s Rule 0 0 1 1 1 1 0 0 0 0 2 6 14 30 60 0 0 1 3 7 15 30 60 Example: Twos Complement Number to Decimal Let a = (1011 1100) 2c be a twos complement number Since a is negative (leftmost bit is 1) Negate a to get a = (0100 0100) 2c Convert a using Horner s rule Horner s Rule 0 1 0 0 0 1 0 0 0 1 2 4 8 18 36 0 1 1 2 4 9 18 36 Therefore, a = (1011 1100) 2c = 36 Converting a Decimal to Twos Complement Algorithm 3 (Conversion of Decimal to Twos Complement) Let (a) 10 be a decimal number 1 If (a) 10 is positive (a) Use repeated remaindering to compute an unsigned binary (b) Append a leftmost 0 to the result 2 If (a) 10 is negative (a) Use repeated remaindering on a to compute an unsigned binary (b) Append a leftmost 0 to the result (c) Use the Negation Algorithm to negate the result 5
Example: Decimal to Twos Complement Notation Let a = +37 be decimal number Notice a = +37 is a signed integer You must account for the + sign in the twos complement representation of a First, use repeated remaindering to convert 37 to an unsigned binary 37 2 (18, 1) 37 = 1 18 2 (9, 0) 37 = 01 9 2 (4, 1) 37 = 101 4 2 (2, 0) 37 = 0101 2 2 (1, 0) 37 = 00101 1 2 (0, 1) 37 = (10 0101) 2 Next, prepend a 0 to represent + +37 = (010 0101) 2c Example: Decimal to Twos Complement Notation Let a = 37 be decimal number Notice a = 37 is a signed integer You must account for the sign in the twos complement representation of a First, as in the previous slide, 37 to an unsigned binary: 37 = (10 0101) 2 Next, prepend a 0 to represent + +37 = (010 0101) 2c Lastly, use the Negation Algorithm do compute: 37 = (101 1011) 2c 6