Survey							
                            
		                
		                * Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
REPRESENTING POSITIVE AND NEGATIVE NUMBERS 1998 Morgan Kaufmann Publishers 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 signmagnitude representation. 1998 Morgan Kaufmann Publishers 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… 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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  1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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. 1998 Morgan Kaufmann Publishers SIGN-MAGNITUDE REPRESENTATION  Multiply the numbers +7 and -5 using 6-bit signedmagnitude representation.   +7 = 000111b -5 = 100101b  7x5 = 0100011b  positive x negative = negative so set the sign bit  Solution: Answer = 1100011b  1998 Morgan Kaufmann Publishers 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!) 1998 Morgan Kaufmann Publishers SIGN-MAGNITUDE REPRESENTATION  A better solution:  Try a different representation for signed binary numbers? 1998 Morgan Kaufmann Publishers 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… 1998 Morgan Kaufmann Publishers TWO’S COMPLEMENT REPRESENTATION  To obtain a two’s complement representation of a negative number... (1) 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 1998 Morgan Kaufmann Publishers EXAMPLE: TWO’S COMPLEMENT REPRESENTATION  What is the 6-bit two’s complement representation of -12?  Solution: (1) unsigned 12 (2) invert bits (3) add 1  -12 = 110100 in 6-bit two’s complement  As with signed magnitude, a 1 in the leftmost bit means negative!    001100 110011 110100 1998 Morgan Kaufmann Publishers 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 000000Discard the 7th (leftmost) overflow bit So our answer is 0. 12 + -12 = 0. Now that’s much better. 1998 Morgan Kaufmann Publishers RANGE OF REPRESENTED VALUES  Four-bit two’s complement: Write 0-7, Fill-in -1, -2, … -8  0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 6 = 0110 7 = 0111          -8 = 1000 -7 = 1001 -6 = 1010 -5 = 1011 -4 = 1100 -3 = 1101 -2 = 1110 -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 1998 Morgan Kaufmann Publishers WHAT ARE MAX_INT AND MIN_INT IN 16 BITS? 1998 Morgan Kaufmann Publishers WHAT ARE MAX_INT AND MIN_INT IN 16 BITS? 1998 Morgan Kaufmann Publishers EXERCISES   2.12 What is the 8-bit two’s complement binary representation for the following decimal numbers? (a) 23 Answer:  (b) -23 Answer:  (c ) 57 Answer:  (d) -57 Answer: Same as unsigned representation 1998 Morgan Kaufmann Publishers See exercise 2.12 on page 39 of Computer Architecture by N. Carter EXERCISES  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 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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: 011100 + 000001 011101 Sign bit is 1 so it’s a negative value. How do we find its absolute value (magnitude)? Negate it by inverting bits and adding 1 Magnitude is 29 but negative sign means -> -29 1998 Morgan Kaufmann Publishers EXERCISES  Convert these 5-bit two’s complement values into decimal.  (a) 11011 Answer:  (b) 10001 Answer:  (c) 11111 Answer:  (d ) 10000 Answer: 1998 Morgan Kaufmann Publishers EXERCISES  Convert these 5-bit two’s complement values into decimal.  (a) 11011 Answer: -5  (b) 10001 Answer: -15  (c) 11111 Answer: -1  (d ) 10000 Answer: -16 1998 Morgan Kaufmann Publishers 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. 1998 Morgan Kaufmann Publishers 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. 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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. 1998 Morgan Kaufmann Publishers PROPERTIES OF TWO’S COMPLEMENT  Multiplication in two’s complement is more complex than with signedmagnitude 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. 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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 Copy original sign bit into new high-order bit 10000001b Clear former sign bit 10000001b (-1 in sign magnitude) 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers 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 1998 Morgan Kaufmann Publishers