Representing Positive and Negative Numbers
Negative numbers The sign (+/-) can be represented using an additional bit known as the sign bit. There are several different methods for encoding the sign. The simplest method is known as sign-magnitude representation.
Sign-magnitude representation Let the high-order bit serve as the sign bit. A positive value has a sign bit of 0. A negative value has a sign bit of 1. Can represent values from -(2(n-1)-1) to +(2(n-1)-1) Exercise: signed-magnitude values for the integers -7 to +7…
Sign-magnitude representation: 4 bits Decimal Signed magnitude +7 0111b +6 0110b +5 0101b +4 0100b +3 0011b +2 0010b +1 0001b +0 0000b -0 1000b -1 1001b -2 1010b -3 1011b -4 1100b -5 1101b -6 1110b -7 1111b There are two possible representations for zero. Negative values have sign bit set to 1
Exercise 2.11 What is the 8-bit signed-magnitude binary representation for each of the following decimal numbers? (a) 23 (b) -23 (c) -48 See exercise 2.11 on page 39 of Computer Architecture by N. Carter
Exercise: Solution 2.11 What is the 8-bit signed-magnitude binary representation for each of the following decimal numbers? (a) 23 00010111b (b) -23 10010111b (c) -48 10110000b The sign bit is highlighted in red See exercise 2.11 on page 39 of Computer Architecture by N. Carter
Sign-magnitude representation Can negate a number simply by inverting the sign bit. Test if a value is positive or negative by checking the sign bit. Easy to perform multiplication or division Just perform unsigned multiplication or division Set sign bit of the result based on sign bits of operands positive x positive = positive positive x negative = negative negative x negative = positive Addition and subtraction present a very difficult problem.
Sign-magnitude representation Multiply the numbers +7 and -5 using 6-bit signed-magnitude representation. +7 = 000111b -5 = 100101b 7x5 = 0100011b positive x negative = negative so set the sign bit Solution: Answer = 1100011b
Sign-magnitude representation Try to directly add 8-bit signed-magnitude values for +10 and -4. Solution: +10 = 00001010b -4 = 10000100b 00001010b + 10000100b 10001110b The sum is 10001110b, which is interpreted as -14 (Wrong!)
Sign-magnitude representation A better solution: Try a different representation for signed binary numbers?
Proposed Solution: Invert the bits 01010 = +9 10101 = -9 Leftmost bit is 1 indicating negative Does addition of 9 + -9 give zero? No? Let’s try something different…
Two’s Complement Representation To obtain a two’s complement representation of a negative number... Find the unsigned binary integer representation (2) Invert each bit (3) Add 1 to the result (4) Discard any overflow bits Two’s complement for a positive number is the same as its unsigned binary representation
Example: Two’s Complement Representation What is the 6-bit two’s complement representation of -12? Solution: (1) unsigned 12 001100 (2) invert bits 110011 (3) add 1 110100 -12 = 110100 in 6-bit two’s complement As with signed magnitude, a 1 in the leftmost bit means negative!
Two’s Complement Representation What is the result of adding +12 and -12 in 6-bit two’s complement? Solution: +12 001100 -12 +110100 ----------------------------------------- 1 000000 Discard the 7th (leftmost) overflow bit So our answer is 0. 12 + -12 = 0. Now that’s much better.
Range of Represented Values Four-bit two’s complement: Write 0-7, Fill-in -1, -2, … -8 0 = 0000 -8 = 1000 1 = 0001 -7 = 1001 2 = 0010 -6 = 1010 3 = 0011 -5 = 1011 4 = 0100 -4 = 1100 5 = 0101 -3 = 1101 6 = 0110 -2 = 1110 7 = 0111 -1 = 1111 There is only one representation for zero Range of values is -8 to +7 inclusive -2^(n-1) to +(2^(n-1))-1
What are Max_int and Min_int in 16 bits?
What are Max_int and Min_int in 16 bits?
Exercises Answer: Same as unsigned representation (a) 23 (b) -23 2.12 What is the 8-bit two’s complement binary representation for the following decimal numbers? (a) 23 Answer: Same as unsigned representation (b) -23 Answer: (c ) 57 (d) -57 See exercise 2.12 on page 39 of Computer Architecture by N. Carter
Exercises (a) 23 Answer: 00010111b Same as unsigned representation 2.12 What is the 8-bit two’s complement binary representation for the following decimal numbers? (a) 23 Answer: 00010111b Same as unsigned representation (b) -23 Answer: 11101001b (c ) 57 Answer: 00111001b (d) -57 Answer: 11000111b See exercise 2.12 on page 39 of Computer Architecture by N. Carter
Negation in Two’s Complement Given a binary number in two’s complement, form its negative by inverting its bits and adding one. This works regardless of whether the original two’s complement binary number is positive or negative (leftmost bit is 1). Negate the 4-bit two’s complement representation of +5 twice. Begin with the two’s complement representation of +5 Negate it, this is the representation of -5 Negate it again, you should be back to +5
From Two’s Complement to Base Ten (a) What is the base ten equivalent for this 6-bit two’s complement value? 011001 Answer: 25 Sign bit is 0, read its value the usual way (b) What is the decimal equivalent of this 6-bit two’s complement value? 100011 Answer: Sign bit is 1 so it’s a negative value. How do we find its absolute value (magnitude)? 011100 Negate it by inverting bits and adding 1 + 000001 011101 Magnitude is 29 but negative sign means -> -29
Exercises Convert these 5-bit two’s complement values into decimal. Answer: (b) 10001 (c) 11111 (d ) 10000
Exercises Convert these 5-bit two’s complement values into decimal. Answer: -5 (b) 10001 Answer: -15 (c) 11111 Answer: -1 (d ) 10000 Answer: -16
Addition in Two’s Complement Addition is correctly computed by directly adding the bits. Compute X-Y by computing X + (-Y) using negation of Y.
Exercises Add the values +3 and -4 in two’s complement notation using 4 bits. Compute -3 - 4 in two’s complement notation using 5-bit numbers.
Exercises: Solution Negate the 4-bit two’s complement representation of +5 twice. 0101 (+5) 1010 + 0001 = 1011 (-5) 0100 + 0001 = 0101 (+5) Negating twice results in the original number as we would expect Add the values +3 and -4 in two’s complement notation. 0011 (+3) 0100 (+4) 1011 + 0001 = 1100 (-4) +1100 (-4) 1111 (-1) Compute -3 - 4 in two’s complement notation. 11101 (-3) +11100 (-4) 11001 (-7) 11001 00110 + 00001 = 00111 (+7) Means to negate the two’s complement value
Useful Properties of Two’s Complement Sign is determined by examining the high-order bit. Negating a number twice results in the original number. Addition is correctly computed by directly adding the bits. Compute X-Y by computing X+(-Y) using negation of Y. Represents values in range -(2(n-1)) to +(2(n-1)-1) Only one representation for zero. Due to these properties, two’s complement notation is used in all modern computers.
Properties of Two’s Complement Multiplication in two’s complement is more complex than with signed-magnitude notation. Addition and subtraction in two’s complement is easier than with signed-magnitude notation. Why would two’s complement still be favored? Answer: Addition and subtraction operations tend to be more frequent than multiplication. Designers choose the fastest way to perform the more frequent computation.
Sign Extension Given two binary numbers of differing numbers of bits. Example: Four bit number and Eight bit number 1001b and 01101100b Sometimes necessary to convert the shorter number to the same number of bits as the larger number before doing arithmetic If the two numbers are unsigned, then simply append extra 0 bits to the high-order end of the shorter number Example: Append an extra four 0 bits to extend to 8 bits 0000 1001b = 00001001b
Sign Extension for Sign-Magnitude Given a binary number expressed in sign-magnitude format Extend the number by appending the extra bits set to 0 Copy the given sign bit into the high-order bit position Set the former sign bit to 0 Example: Sign extend the 4-bit sign-magnitude number 1001b to 8 bits. This is -1 (base 10) in sign magnitude. 1001b 1000 1001b 10000001b Copy original sign bit into new high-order bit Clear former sign bit 10000001b (-1 in sign magnitude)
Sign Extension for Sign-Magnitude Problem: What is the 16-bit sign-magnitude representation of the 8-bit sign-magnitude 10000111 (-7)? Solution: 00000000 10000111 // Preppend extra 0 bits 10000000 00000111 // Move original sign bit into new // high-order bit position 1000000000000111
Sign Extension for Two’s Complement Given 2’s complement binary number Extend the number by appending the extra bits Set all of the extra bits to the original sign bit value Example: Sign-extend the 4-bit two’s complement number 1001b to 8 bits. 1001b 11111001b
Sign Extension for Two’s Complement Problem: What is the 16-bit sign extension of the 8-bit two’s complement value 10010010 (-110)? Solution: 00000000 10010010 // Preppend 8 extra bits 11111111 10010010 // Set extra bits to original sign // bit value 11111111 10010010