4.12 data representation

19
30/03/22 30/03/22 1 4.12 Data Representation Two’s Complement Two’s Complement & & Binary Arithmetic Binary Arithmetic

Upload: josiah-burks

Post on 30-Dec-2015

79 views

Category:

Documents


0 download

DESCRIPTION

4.12 Data Representation. Two’s Complement & Binary Arithmetic. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 4.12 Data Representation

19/04/2319/04/23 11

4.12 Data Representation

Two’s ComplementTwo’s Complement

&&

Binary ArithmeticBinary Arithmetic

Page 2: 4.12 Data Representation

2219/04/2319/04/23

Learning Objectives: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.

Page 3: 4.12 Data Representation

3319/04/2319/04/23

Representing Negative Numbers - Sign & Magnitude

As there is no third symbol available to As there is no third symbol available to store a negative symbol explicitly we must store a negative symbol explicitly we must use a bit to show if a number is negative use a bit to show if a number is negative or not.or not. We name this bit the ‘We name this bit the ‘Sign BitSign Bit’’ We use the We use the leftmostleftmost bit. bit. If the ‘Sign Bit’ is If the ‘Sign Bit’ is 1 1 then the number is then the number is

negative, if it is negative, if it is 00 then it is positive. then it is positive.

Page 4: 4.12 Data Representation

4419/04/2319/04/23

Representing Negative Numbers- Sign & Magnitude

At first glance it may appear to be simple At first glance it may appear to be simple from this point, for example:from this point, for example:

3 = 0 00000113 = 0 0000011

soso-3 = 1 0000011-3 = 1 0000011

Sign BitsSign Bits

Page 5: 4.12 Data Representation

5519/04/2319/04/23

Binary – Decimal Spreadsheet Binary – Decimal Spreadsheet Converter 2Converter 2

Try using it to ‘play’ with sign and Try using it to ‘play’ with sign and magnitude binary numbers.magnitude binary numbers.

Page 6: 4.12 Data Representation

6619/04/2319/04/23

Binary Arithmetic RulesBinary Arithmetic Rules

0 + 0 = 00 + 0 = 0

0 + 1 = 10 + 1 = 1

1 + 0 = 11 + 0 = 1

1 + 1 = 0 (carry 1)1 + 1 = 0 (carry 1)

1+1+1 = 1 (carry 1)1+1+1 = 1 (carry 1)

Page 7: 4.12 Data Representation

7719/04/2319/04/23

Problems with Sign & Magnitude

To correct this error:To correct this error:

Addition and subtraction calculations give incorrect Addition and subtraction calculations give incorrect results:results: e.g. +3 + -3 = 0e.g. +3 + -3 = 0

but but 0 0000011 0 000000110 0000011 0 00000011 +1 0000011+1 0000011 - -1 000000111 00000011 1 0000110 i.e. 1 0000110 i.e. not 0 not 0 0 0 0000000000000000

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 If the signs are the same, add the magnitudes as unsigned numbers and, if there is a “carry out”, use it as the MSB there is a “carry out”, use it as the MSB (last bit)(last bit)..If the signs differ, subtract the smaller magnitude from the larger, and keep the If the signs differ, subtract the smaller magnitude from the larger, and keep the sign of the larger e.g. sign of the larger e.g. http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.htmlhttp://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.html

Page 8: 4.12 Data Representation

8819/04/2319/04/23

Problems with Sign & Magnitude

Also note that this method allows two Also note that this method allows two representations of 0:representations of 0: 1 00000001 0000000 0 00000000 0000000

This will cause problems for a processor.This will cause problems for a processor.

Page 9: 4.12 Data Representation

9919/04/2319/04/23

Two’s ComplementTwo’s Complement

This is a better way to represent negative This is a better way to represent negative numbers.numbers.

Imagine a km clock in a car set at Imagine a km clock in a car set at 00000000 kilometres.00000000 kilometres. If the car goes forward one km the reading If the car goes forward one km the reading

becomes 00000001.becomes 00000001. If the meter was turned back one km the If the meter was turned back one km the

reading would be 99999999 km.reading would be 99999999 km. This could be interpreted as ‘-1’ km.This could be interpreted as ‘-1’ km.

Page 10: 4.12 Data Representation

101019/04/2319/04/23

Two’s ComplementTwo’s Complement

So:So: 0 0000011 = 30 0000011 = 3 0 0000010 = 20 0000010 = 2 0 0000001 = 10 0000001 = 1 0 0000000 = 00 0000000 = 0 1 1111111 = -11 1111111 = -1 1 1111110 = -21 1111110 = -2 1 1111101 = -31 1111101 = -3

Sign BitSign Bit

Page 11: 4.12 Data Representation

111119/04/2319/04/23

Binary – Decimal Spreadsheet Binary – Decimal Spreadsheet Converter 2Converter 2

Try using it to ‘play’ with two’s complement Try using it to ‘play’ with two’s complement binary numbers.binary numbers.

Page 12: 4.12 Data Representation

121219/04/2319/04/23

Negative denary to binaryNegative denary to binary Work out the binary number as if it were Work out the binary number as if it were

positive.positive. From the left, flip all bits up to the last ‘1’, From the left, flip all bits up to the last ‘1’,

leave this and any other bits after that alone.leave this and any other bits after that alone.Flip means change 0 to 1 Flip means change 0 to 1 oror 1 to 0. 1 to 0.

Negative binary to denaryNegative binary to denary Reverse of aboveReverse of above

Using Two’s ComplementUsing Two’s Complement

Page 13: 4.12 Data Representation

131319/04/2319/04/23

-5-5

Work out the binary number as if it were Work out the binary number as if it were positive.positive. 5 = 0 00001015 = 0 0000101

From the left, flip all bits up to the last ‘1’, From the left, flip all bits up to the last ‘1’, leave this and any other bits after that leave this and any other bits after that alone.alone.

-5 = 11111011-5 = 11111011

11 111111110011Don’t flip the last 1.

Don’t flip the last 1.

11

Page 14: 4.12 Data Representation

141419/04/2319/04/23

1111101111111011

From the left, flip all bits up to the last ‘1’, leave From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone.this and any other bits after that alone. 0000010100000101

Work out the decimal number as if it were Work out the decimal number as if it were positive.positive. 0 0000101 = 50 0000101 = 5

Add the minus sign.Add the minus sign.

11111011 = -511111011 = -5

Page 15: 4.12 Data Representation

151519/04/2319/04/23

The MSB stays as a number, but is made The MSB stays as a number, but is made negative. This means that the column headings negative. This means that the column headings areare

-128-128 64 64 32 32 1616 88 44 22 11

+117 does not need to use the MSB, so it stays as +117 does not need to use the MSB, so it stays as 01110101.01110101.-117 = -128 + 11-117 = -128 + 11 = -128 + (8 + 2 + 1)= -128 + (8 + 2 + 1)Fitting this in the columns gives 10001011Fitting this in the columns gives 10001011

Alternative way of using Two’s Alternative way of using Two’s ComplementComplement

Page 16: 4.12 Data Representation

161619/04/2319/04/23

Two’s ComplementTwo’s Complement

Now addition and subtraction calculations give the correct Now addition and subtraction calculations give the correct results:results:

0 0000011 = 30 0000011 = 3+ + 1 1111101 = -31 1111101 = -3 10 0000000 = 010 0000000 = 0 11 111111 11 111111 <- carries<- carries

Notes: Notes: The last ‘carry’ of 1 The last ‘carry’ of 1 (carry in and out of the (carry in and out of the MMost ost SSignificant ignificant BBit – it – MSBMSB)) has to has to be ignored unless an overflow has occurred be ignored unless an overflow has occurred (see next slide)(see next slide)..The arithmetic works here, as The arithmetic works here, as all the bits, including the sign bit, in this all the bits, including the sign bit, in this method have a place value.method have a place value.There is only one representation for zero.There is only one representation for zero.

00000000 = 000000000 = 0 10000000 = -128 (not 0 as in sign & magnitude)10000000 = -128 (not 0 as in sign & magnitude)

Page 17: 4.12 Data Representation

+102 = 01100110+117 = 01110101

102+117 = 219but

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

Problem with Two’s ComplementTwo’s Complement

New Sign bit (-256)old sign bit = +128

New Sign bit (-256)old sign bit = +128

Page 18: 4.12 Data Representation

181819/04/2319/04/23

PlenaryPlenary

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.

Page 19: 4.12 Data Representation

191919/04/2319/04/23

PlenaryPlenary

1. -46

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