Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representing Positive and Negative Numbers

Similar presentations


Presentation on theme: "Representing Positive and Negative Numbers"— Presentation transcript:

1 Representing Positive and Negative Numbers

2 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.

3 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…

4 Sign-magnitude representation: 4 bits
Decimal Signed magnitude b b b b b b b b b b b b b b b b There are two possible representations for zero. Negative values have sign bit set to 1

5 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

6 Exercise: Solution 2.11 What is the 8-bit signed-magnitude binary representation for each of the following decimal numbers? (a) 23 b (b) -23 b (c) -48 b The sign bit is highlighted in red See exercise 2.11 on page 39 of Computer Architecture by N. Carter

7 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.

8 Sign-magnitude representation
Multiply the numbers +7 and -5 using 6-bit signed-magnitude representation. +7 = b -5 = b 7x5 = b positive x negative = negative so set the sign bit Solution: Answer = b

9 Sign-magnitude representation
Try to directly add 8-bit signed-magnitude values for +10 and -4. Solution: +10 = b -4 = b b b b The sum is b, which is interpreted as -14 (Wrong!)

10 Sign-magnitude representation
A better solution: Try a different representation for signed binary numbers?

11 Proposed Solution: Invert the bits
01010 = = -9 Leftmost bit is 1 indicating negative Does addition of give zero? No? Let’s try something different…

12 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

13 Example: Two’s Complement Representation
What is the 6-bit two’s complement representation of -12? Solution: (1) unsigned (2) invert bits (3) add -12 = in 6-bit two’s complement As with signed magnitude, a 1 in the leftmost bit means negative!

14 Two’s Complement Representation
What is the result of adding +12 and -12 in 6-bit two’s complement? Solution: Discard the 7th (leftmost) overflow bit So our answer is 0. = 0. Now that’s much better.

15 Range of Represented Values
Four-bit two’s complement: Write 0-7, Fill-in -1, -2, … -8 0 = = 1000 1 = = 1001 2 = = 1010 3 = = 1011 4 = = 1100 5 = = 1101 6 = = 1110 7 = = 1111 There is only one representation for zero Range of values is -8 to +7 inclusive -2^(n-1) to +(2^(n-1))-1

16 What are Max_int and Min_int in 16 bits?

17 What are Max_int and Min_int in 16 bits?

18 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

19 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: b Same as unsigned representation (b) -23 Answer: b (c ) 57 Answer: b (d) -57 Answer: b See exercise 2.12 on page 39 of Computer Architecture by N. Carter

20 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

21 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)? Negate it by inverting bits and adding 1 Magnitude is 29 but negative sign means -> -29

22 Exercises Convert these 5-bit two’s complement values into decimal.
Answer: (b) 10001 (c) 11111 (d ) 10000

23 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

24 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.

25 Exercises Add the values +3 and -4 in two’s complement notation using 4 bits. Compute in two’s complement notation using 5-bit numbers.

26 Exercises: Solution Negate the 4-bit two’s complement representation of +5 twice. 0101 (+5)  = 1011 (-5)  = 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)  = 1100 (-4) +1100 (-4) 1111 (-1) Compute in two’s complement notation. 11101 (-3) (-4) 11001 (-7)  = (+7)  Means to negate the two’s complement value

27 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.

28 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.

29 Sign Extension Given two binary numbers of differing numbers of bits.
Example: Four bit number and Eight bit number 1001b and b 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 = b

30 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 b b Copy original sign bit into new high-order bit Clear former sign bit b (-1 in sign magnitude)

31 Sign Extension for Sign-Magnitude
Problem: What is the 16-bit sign-magnitude representation of the 8-bit sign-magnitude (-7)? Solution: // Preppend extra 0 bits // Move original sign bit into new // high-order bit position

32 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 b

33 Sign Extension for Two’s Complement
Problem: What is the 16-bit sign extension of the 8-bit two’s complement value (-110)? Solution: // Preppend 8 extra bits // Set extra bits to original sign // bit value


Download ppt "Representing Positive and Negative Numbers"

Similar presentations


Ads by Google