two’s complement. a system used to represent a negative number in binary a system used to...

22
Two’s Complement Two’s Complement

Upload: william-burns

Post on 17-Jan-2016

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Two’s ComplementComplement

Page 2: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s ComplementTwo’s Complement

A system used to represent a A system used to represent a negative number in binarynegative number in binary

Positive numbers start with a 0Positive numbers start with a 0 Negative numbers start with a 1Negative numbers start with a 1

Page 3: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

On a cars Mileometer if you On a cars Mileometer if you went backwards this would went backwards this would

happen:happen:

0 0 0 0 0 0 0 32109 9 9 9 9 9 9 99 9 9 9 9 9 9 89 9 9 9 9 9 9 79 9 9 9 9 9 9 6

Page 4: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

0 0 0 0 0 0 1 1

Positive MSB (Most Significant Bit) indicated by 0

= 3

128 64 32 16 8 4 2 1

Page 5: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

0 0 0 0 0 0 1 0

Positive MSB indicated by 0

= 2

128 64 32 16 8 4 2 1

Page 6: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

0 0 0 0 0 0 0 1

Positive MSB indicated by 0

= 1

128 64 32 16 8 4 2 1

Page 7: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

0 0 0 0 0 0 0 0

Positive MSB indicated by 0

= 0

128 64 32 16 8 4 2 1

Page 8: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

1 1 1 1 1 1 1 1

Negative MSB indicated by 1

= -1

-128 64 32 16 8 4 2 1

Page 9: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

1 1 1 1 1 1 1 0

Negative MSB indicated by 1

= -2

-128 64 32 16 8 4 2 1

Page 10: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Two’s Complement is like a Two’s Complement is like a Speedometer…….Speedometer…….

1 1 1 1 1 1 0 1

Negative MSB indicated by 1

= -3

-128 64 32 16 8 4 2 1

Page 11: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

How can you convert a How can you convert a negative two’s complement negative two’s complement

number to denary?number to denary?

Example: Convert two’s complement Example: Convert two’s complement 1001011010010110

-128-128 64 64 32 16 32 16 8 8 4 4 22 11

11 0 0 1 0 1 1 0 0 0 1 0 1 1 0

This would be:This would be:

-128 + 16 + 4 + 2 = -106-128 + 16 + 4 + 2 = -106

Page 12: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Example 2Example 2

Convert two’s complement 11011011Convert two’s complement 11011011

-128-128 64 64 32 16 32 16 8 8 4 4 2211

11 1 0 1 1 0 1 1 1 0 1 1 0 1 1

This would be:This would be:-128 + 64 + 16 + 8 + 2 + 1 = -37-128 + 64 + 16 + 8 + 2 + 1 = -37

Page 13: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

ActivityActivity

Convert the following numbers to Convert the following numbers to denary:denary:

1.1. Two’s complement 00110011Two’s complement 00110011

2.2. Two’s complement 11001100Two’s complement 11001100

3.3. Two’s complement 10111101Two’s complement 10111101

4.4. Binary (no +\- MSB at the start) Binary (no +\- MSB at the start) 1111010111110101

Page 14: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

How can you convert a negative How can you convert a negative denary number into two’s denary number into two’s

complement?complement?Step 1Step 1

pretend negative denary number is pretend negative denary number is positive and convert it into binary e.g. positive and convert it into binary e.g. -52 becomes 52 -52 becomes 52

128128 64 64 32 16 32 16 8 8 4 4 2211

0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 0

Page 15: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

How can you convert a negative How can you convert a negative denary number into two’s denary number into two’s

complement?complement?Step 2Step 2

Starting from the right, leave all Starting from the right, leave all digits alone, up to and including the digits alone, up to and including the first 1, then change all the other first 1, then change all the other digits around (0 to 1) or (1 to 0)digits around (0 to 1) or (1 to 0)

126 64126 64 32 16 32 16 8 8 4 4 2211

0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 0

00110011

Page 16: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Alternative Method:Alternative Method:Create positive binary number as before:Create positive binary number as before:

128128 6464 32 16 32 16 8 4 8 4 2 2 11

0 0 1 1 0 1 0 0 0 0 1 1 0 1 0 0 Next change all 1’s and 0’s around:Next change all 1’s and 0’s around:128128 64 64 32 16 32 16 8 4 2 8 4 2 1 1 1 1 0 0 1 0 1 11 1 0 0 1 0 1 1Then add 1 to the number:Then add 1 to the number:128128 64 64 32 16 8 32 16 8 4 2 4 2 1 1 1 1 0 0 1 0 1 11 1 0 0 1 0 1 1+ + 0 0 0 0 0 0 0 10 0 0 0 0 0 0 1= = 1 1 0 0 1 1 0 01 1 0 0 1 1 0 0

Page 17: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Addition in BinaryAddition in Binary

0 1 0 0 1 1 0 10 1 0 0 1 1 0 1

+ + 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 00010001

111111

1248163264128

77

59

136

Denary

Page 18: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Addition ExerciseAddition Exercise

Add 47 to 159 in binaryAdd 47 to 159 in binary Add 12 to 47 in binaryAdd 12 to 47 in binary

Page 19: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Subtraction in Binary Subtraction in Binary

Rather than subtracting, convert the Rather than subtracting, convert the number that is to be subtracted into number that is to be subtracted into a negative number then add them a negative number then add them together as beforetogether as before

Page 20: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Subtraction in Binary Subtraction in Binary

e.g. 15-12e.g. 15-12

12 = 0000110012 = 00001100

-12 = 11110100-12 = 11110100

15 = 0000111115 = 00001111

0000111100001111

+ + 1111010011110100

(1) 00000011(1) 00000011

The carry of 1 is ignored

Page 21: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Subtraction ExercisesSubtraction Exercises

1.1. Subtract 23 from 123 in binarySubtract 23 from 123 in binary

2.2. Subtract 14 from 32 in binarySubtract 14 from 32 in binary

Page 22: Two’s Complement. A system used to represent a negative number in binary A system used to represent a negative number in binary Positive numbers start

Rules:Rules:

Positive numbers have a 0 as the MSBPositive numbers have a 0 as the MSB Negative numbers have a 1 as the MSBNegative numbers have a 1 as the MSB Even numbers have 0 as the LSB (Least Even numbers have 0 as the LSB (Least

Significant Bit)Significant Bit) Odd numbers have 1 as the LSBOdd numbers have 1 as the LSB -1 is always represented by a 1 in every -1 is always represented by a 1 in every

bit regardless of number of bitsbit regardless of number of bits