09/03/20161 Information Representation Two’s Complement & Binary Arithmetic
209/03/2016 Learning Objectives: Express a positive or negative integer in two’s complement form. Perform integer binary arithmetic: addition and subtraction.
309/03/2016 Two’s Complement This is a better way to represent negative numbers. Imagine a km clock in a car set at kilometres. If the car goes forward one km the reading becomes If the car goes forward one km the reading becomes If the meter was turned back one km the reading would be km. If the meter was turned back one km the reading would be km. This could be interpreted as ‘-1’ km. This could be interpreted as ‘-1’ km.
409/03/2016 Two’s Complement So: = = = = = = = = = = = = = = -3 Sign Bit
509/03/2016 Binary – Decimal Spreadsheet Converter 2 Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers.
609/03/2016 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
709/03/ Work out the binary number as if it were positive. 5 = = From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. -5 = Don’t flip the last 1. 1
809/03/ From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone Work out the decimal number as if it were positive = = 5 Add the minus sign = -5
909/03/2016 The MSB stays as a number, but is made negative. This means that the column headings are does not need to use the MSB, so it stays as = = ( ) = ( ) Fitting this in the columns gives Alternative way of using Two’s Complement
1009/03/2016 Binary Arithmetic Rules = = = = 0 (carry 1) = 1 (carry 1)
1109/03/2016 Two’s Complement Now addition and subtraction calculations give the correct results: = = = = = <- carries <- 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 = = = -128 (not 0 as in sign & magnitude) = -128 (not 0 as in sign & magnitude)
+102 = = = 219 but = <- 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: = 219 (which is correct). Two’s Complement Problem with Two’s Complement New Sign bit (-256) old sign bit = New Sign bit (-256) old sign bit = + 128
1309/03/2016 Two’s Complement Also note that: There is only one representation for zero. There is only one representation for zero.
1409/03/2016 Plenary A particular computer stores numbers as 8 bit, two’s complement, binary numbers and are two numbers stored in the computer Write down the decimal equivalent of Add the two binary values together and comment on your answer.
1509/03/2016 Plenary = +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).