Presentation is loading. Please wait.

Presentation is loading. Please wait.

07/12/20151 4.12 Data Representation Two’s Complement & Binary Arithmetic.

Similar presentations


Presentation on theme: "07/12/20151 4.12 Data Representation Two’s Complement & Binary Arithmetic."— Presentation transcript:

1 07/12/20151 4.12 Data Representation Two’s Complement & Binary Arithmetic

2 207/12/2015 Learning Objectives: Describe and use two's complement and sign and magnitude to represent positive and negative integers. Perform integer binary arithmetic: addition and subtraction.

3 307/12/2015 Representing Negative Numbers - Sign & Magnitude As there is no third symbol available to store a negative symbol explicitly we must use a bit to show if a number is negative or not. We name this bit the ‘Sign Bit’ We name this bit the ‘Sign Bit’ We use the leftmost bit. We use the leftmost bit. If the ‘Sign Bit’ is 1 then the number is negative, if it is 0 then it is positive. If the ‘Sign Bit’ is 1 then the number is negative, if it is 0 then it is positive.

4 407/12/2015 Representing Negative Numbers - Sign & Magnitude At first glance it may appear to be simple from this point, for example: 3 = 0 0000011 3 = 0 0000011 so -3 = 1 0000011 so -3 = 1 0000011 Sign Bits

5 507/12/2015 Binary – Decimal Spreadsheet Converter 2 Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with sign and magnitude binary numbers.

6 607/12/2015 Binary Arithmetic Rules 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 (carry 1) 1+1+1 = 1 (carry 1)

7 707/12/2015 Problems with Sign & Magnitude To correct this error: Addition and subtraction calculations give incorrect results: e.g. +3 + -3 = 0 e.g. +3 + -3 = 0but 0 0000011 0 00000011 0 0000011 0 00000011 +1 0000011 -1 00000011 +1 0000011 -1 00000011 1 0000110 i.e. not 0 0 00000000 1 0000110 i.e. not 0 0 00000000 This is due to the fact that the sign bit does not have a place value. This is due to the fact that the sign bit does not have a place value. Rules to correct this error: Rules to correct this error: If the signs are the same, add the magnitudes as unsigned numbers and, if there is a “carry out”, use it as the MSB (last bit). If the signs differ, subtract the smaller magnitude from the larger, and keep the sign of the larger e.g. http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.html http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.html

8 807/12/2015 Problems with Sign & Magnitude Also note that this method allows two representations of 0: 1 0000000 1 0000000 0 0000000 0 0000000 This will cause problems for a processor.

9 907/12/2015 Two’s Complement This is a better way to represent negative numbers. Imagine a km clock in a car set at 00000000 kilometres. If the car goes forward one km the reading becomes 00000001. If the car goes forward one km the reading becomes 00000001. If the meter was turned back one km the reading would be 99999999 km. If the meter was turned back one km the reading would be 99999999 km. This could be interpreted as ‘-1’ km. This could be interpreted as ‘-1’ km.

10 1007/12/2015 Negative denary to binary Work out the binary number as if it were positive. Work out the binary number as if it were positive. From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. Flip means change 0 to 1 or 1 to 0. Negative binary to denary Reverse of above Reverse of above Using Two’s Complement

11 1107/12/2015 Two’s Complement So: 0 0000011 = 3 0 0000011 = 3 0 0000010 = 2 0 0000010 = 2 0 0000001 = 1 0 0000001 = 1 0 0000000 = 0 0 0000000 = 0 1 1111111 = -1 1 1111111 = -1 1 1111110 = -2 1 1111110 = -2 1 1111101 = -3 1 1111101 = -3 Sign Bit

12 1207/12/2015 Binary – Decimal Spreadsheet Converter 2 Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers.

13 1307/12/2015 -5 Work out the binary number as if it were positive. 5 = 0 0000101 5 = 0 0000101 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. -5 = 11111011 1111101 Don’t flip the last 1. 1

14 1407/12/2015 11111011 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. 00000101 00000101 Work out the decimal number as if it were positive. 0 0000101 = 5 0 0000101 = 5 Add the minus sign. 11111011 = -5

15 1507/12/2015 The MSB stays as a number, but is made negative. This means that the column headings are -128 64 32168421 +117 does not need to use the MSB, so it stays as 01110101. -117 = -128 + 11 = -128 + (8 + 2 + 1) = -128 + (8 + 2 + 1) Fitting this in the columns gives 10001011 Alternative way of using Two’s Complement

16 1607/12/2015 Two’s Complement Now addition and subtraction calculations give the correct results: 0 0000011 = 3 0 0000011 = 3 + 1 1111101 = -3 10 0000000 = 0 10 0000000 = 0 11 111111 <- carries 11 111111 <- carries Notes: Notes: The last ‘carry’ of 1 (carry in and out of the Most Significant Bit – MSB) has to be ignored unless an overflow has occurred (see next slide). The arithmetic works here, as all the bits, including the sign bit, in this method have a place value. There is only one representation for zero. 00000000 = 0 00000000 = 0 10000000 = -128 (not 0 as in sign & magnitude) 10000000 = -128 (not 0 as in sign & magnitude)

17 +102 = 01100110 +117 = 01110101 102+117 = 219 but 01100110 + 01110101 11011011 = -37 11 1 <- carries The original numbers are positive but the answer is negative! There has been an overflow from the positive part of the byte to the negative. To solve this error: If an "overflow" occurs add an extra bit and use this as the new sign bit. An overflow in a two's complement sum has occurred if: The sum of two positive numbers gives a negative result. The sum of two negative numbers gives a positive result. e.g. For the example above: 011011011 = 219 (which is correct). Two’s Complement Problem with Two’s Complement New Sign bit (-256) old sign bit = + 128 New Sign bit (-256) old sign bit = + 128

18 1807/12/2015 Two’s Complement Also note that: There is only one representation for zero. There is only one representation for zero.

19 1907/12/2015 Plenary A particular computer stores numbers as 8 bit, two’s complement, binary numbers. 01011101 and 11010010 are two numbers stored in the computer. 1. 1.Write down the decimal equivalent of 11010010. 2. 2.Add the two binary values together and comment on your answer.

20 2007/12/2015 Plenary 1. 1.-46 2. 2.00101111 = +47 A positive and negative have been added together and the result is positive. Because the larger value was positive. There was carry in and out of MSB therefore ignore carry out, (result is correct).


Download ppt "07/12/20151 4.12 Data Representation Two’s Complement & Binary Arithmetic."

Similar presentations


Ads by Google