Information Representation Twos Complement Binary Arithmetic 2262021 1
Information Representation Two’s Complement & Binary Arithmetic 2/26/2021 1
Learning Objectives: Express a positive or negative integer in two’s complement form. Perform integer binary arithmetic: addition and subtraction. 2/26/2021 2
Two’s Complement This is a better way to represent negative numbers. Imagine a km clock in a car set at 0000 kilometres. n n n 2/26/2021 If the car goes forward one km the reading becomes 00000001. If the meter was turned back one km the reading would be 9999 km. This could be interpreted as ‘-1’ km. 3
Two’s Complement So: n n n n 0 0000011 = 3 0 0000010 = 2 0 0000001 = 1 0 0000000 = 0 1 1111111 = -1 1 1111110 = -2 1 1111101 = -3 Sign Bit 2/26/2021 4
Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers. 2/26/2021 5
Using Two’s Complement Negative denary to binary n n 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. Flip means change 0 to 1 or 1 to 0. Negative binary to denary n 2/26/2021 Reverse of above 6
-5 Work out the binary number as if it were positive. n 5 = 0 0000101 From the left, flip all bits up to the last ‘ 1’, leave this and any other bits after that alone. Don’t flip 1 11 1 101 1 the last 1. -5 = 11111011 2/26/2021 7
11111011 From the left, flip all bits up to the last ‘ 1’, leave this and any other bits after that alone. n 00000101 Work out the decimal number as if it were positive. n 0 0000101 = 5 Add the minus sign. 11111011 = -5 2/26/2021 8
Alternative way of using Two’s Complement The MSB stays as a number, but is made negative. This means that the column headings are -128 64 32 16 8 4 2 1 +117 does not need to use the MSB, so it stays as 01110101. -117 = -128 + 11 = -128 + (8 + 2 + 1) Fitting this in the columns gives 10001011 2/26/2021 9
Binary Arithmetic Rules 0+0=0 0+1=1 1+0=1 1 + 1 = 0 (carry 1) 1+1+1 = 1 (carry 1) 2/26/2021 10
Two’s Complement Now addition and subtraction calculations give the correct results: 0 0000011 = 3 + 1 1111101 = -3 10 0000000 = 0 11 111111 <- carries n n 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. n n 2/26/2021 0000 = 0 10000000 = -128 (not 0 as in sign & magnitude) 11
Problem with Two’s Complement +102 = 0110 +117 = 01110101 n 102+117 = 219 but n + 0110 01110101 11011011 = -37 11 1 <- carries New Sign bit (-256) old sign bit = +128 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: n 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: n n 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 Also note that: n 2/26/2021 There is only one representation for zero. 13
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. Write down the decimal equivalent of 11010010. 2. Add the two binary values together and comment on your answer. 2/26/2021 14
Plenary 1. -46 2. 00101111 = +47 n n n 2/26/2021 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). 15
- Slides: 15