Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two’s Complement & Binary Arithmetic

Similar presentations


Presentation on theme: "Two’s Complement & Binary Arithmetic"— Presentation transcript:

1 Two’s Complement & Binary Arithmetic
4.12 Data Representation Two’s Complement & Binary Arithmetic 29/09/2019

2 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. 29/09/2019

3 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 use the leftmost bit. If the ‘Sign Bit’ is 1 then the number is negative, if it is 0 then it is positive. 29/09/2019

4 Representing Negative Numbers - Sign & Magnitude
At first glance it may appear to be simple from this point, for example: 3 = so -3 = Sign Bits 29/09/2019

5 Binary – Decimal Spreadsheet Converter 2
Try using it to ‘play’ with sign and magnitude binary numbers. 29/09/2019 5

6 Binary Arithmetic Rules
0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 (carry 1) 1+1+1 = 1 (carry 1) 29/09/2019

7 Problems with Sign & Magnitude
Addition and subtraction calculations give incorrect results: e.g = 0 but i.e. not This is due to the fact that the sign bit does not have a place value. 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. To correct this error: 29/09/2019

8 Problems with Sign & Magnitude
Also note that this method allows two representations of 0: This will cause problems for a processor. 29/09/2019 8

9 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 meter was turned back one km the reading would be km. This could be interpreted as ‘-1’ km. 29/09/2019

10 Two’s Complement Sign Bit So: 0 0000011 = 3 0 0000010 = 2
= 1 = 0 = -1 = -2 = -3 Sign Bit 29/09/2019

11 Binary – Decimal Spreadsheet Converter 2
Try using it to ‘play’ with two’s complement binary numbers. 29/09/2019

12 Using Two’s Complement
Negative denary to binary 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 Reverse of above 29/09/2019

13 -5 -5 = 11111011 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 1 1 1 1 1 1 29/09/2019

14 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 29/09/2019

15 Alternative way of using Two’s Complement
The MSB stays as a number, but is made negative. This means that the column headings are +117 does not need to use the MSB, so it stays as -117 = = ( ) Fitting this in the columns gives 29/09/2019

16 Two’s Complement Now addition and subtraction calculations give the correct results: = 3 = -3 = 0 <- carries 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. = 0 = -128 (not 0 as in sign & magnitude) 29/09/2019

17 Problem with Two’s Complement
+102 = +117 = = 219 but = -37 <- 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). New Sign bit (-256) old sign bit = +128

18 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. 29/09/2019

19 Plenary -46 = +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). 29/09/2019


Download ppt "Two’s Complement & Binary Arithmetic"

Similar presentations


Ads by Google