0% found this document useful (0 votes)
65 views

Twos Comp PDF

The document introduces 2's complement representation of integers. [1] 2's complement solves problems with 1's complement by removing negative zero and expanding the range of representable numbers. [2] To convert a positive number to its negative equivalent in 2's complement, its 1's complement is taken and then 1 is added. [3] Addition and subtraction of numbers in 2's complement works the same as unsigned binary addition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Twos Comp PDF

The document introduces 2's complement representation of integers. [1] 2's complement solves problems with 1's complement by removing negative zero and expanding the range of representable numbers. [2] To convert a positive number to its negative equivalent in 2's complement, its 1's complement is taken and then 1 is added. [3] Addition and subtraction of numbers in 2's complement works the same as unsigned binary addition.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to 2’s Complement

Michael Burrell

July 12, 2019


What we know

I Integers (whole numbers) are stored in binary (1s and 0s)


Binary
0 000
1 001
2 010
3 011
.. ..
. .
7 111
What we know

I Integers (whole numbers) are stored in binary (1s and 0s)


Binary
0 000
1 001
2 010
3 011
.. ..
. .
7 111
I The number of bits dictates the maximum value
1 bit: 0–1 4 bits: 0–15
2 bits: 0–3 5 bits: 0–31
3 bits: 0–7 n bits: 0–(2n − 1)
What we know

I We know how to add 2 numbers!


0 0 1 1 3
+ 0 1 1 0 6
What we know

I We know how to add 2 numbers!


0 0 1 1 3
+ 0 1 1 0 6
= 1 0 0 1 9
What we don’t know

I We can represent positive integers (and 0)


I For any positive integer, we can represent it (given enough
bits)
I We can’t represent negatives yet
What we don’t know

I We can represent positive integers (and 0)


I For any positive integer, we can represent it (given enough
bits)
I We can’t represent negatives yet
I There are several conventions for representing negatives
I Sign-magnitude
I Bias
I 1’s complement (learning now!)
I 2’s complement (will learn in a couple minutes!)
I ...
I Before we get to 2’s complement, we first need to know 1’s
complement
Making a negative number in 1’s complement

0 0 1 1

Step 1: Start with a positive number (in this case, 0011 = 3)


Making a negative number in 1’s complement

0 0 1 1

Step 2: The leftmost bit is the sign bit. 0 means “positive” and 1
means “negative”
Making a negative number in 1’s complement

0 0 1 1

Step 3: To make it negative, flip every bit. “Flip” means change 0


to 1, and change 1 to 0.
Making a negative number in 1’s complement

1 0 1 1

Step 3: To make it negative, flip every bit. “Flip” means change 0


to 1, and change 1 to 0.
Making a negative number in 1’s complement

1 1 1 1

Step 3: To make it negative, flip every bit. “Flip” means change 0


to 1, and change 1 to 0.
Making a negative number in 1’s complement

1 1 0 1

Step 3: To make it negative, flip every bit. “Flip” means change 0


to 1, and change 1 to 0.
Making a negative number in 1’s complement

1 1 0 0

Step 3: To make it negative, flip every bit. “Flip” means change 0


to 1, and change 1 to 0.
Making a negative number in 1’s complement

1 1 0 0

We have now constructed -3. 1100 = -3.


1’s complement in 3 bits

Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3
1’s complement in 3 bits

Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3

I The range of positive numbers shrank when we added


negatives
I Before, with 3 bits, we could represent 0–7
I Now we can represent -3–3
1’s complement in 3 bits

Value
0 0 0 0
1 1 1 -0
0 0 1 1
1 1 0 -1
0 1 0 2
1 0 1 -2
0 1 1 3
1 0 0 -3

I The range of positive numbers shrank when we added


negatives
I Before, with 3 bits, we could represent 0–7
I Now we can represent -3–3
I What the heck is negative zero??
1’s complement

I It works!
I It is awkward in a couple ways:
I It has a representation for negative zero
I Making an electronic circuit to do addition and subtraction is
complex
1’s complement

I It works!
I It is awkward in a couple ways:
I It has a representation for negative zero
I Making an electronic circuit to do addition and subtraction is
complex
I But, it’s close to what we want. . .
2’s complement

I 2’s complement is a system that builds on 1’s complement


I It solves the two problems that 1’s complement had
Making a negative number in 2’s complement

0 0 1 1

Step 1: Start with a positive number (in this case, 0011 = 3)


Making a negative number in 2’s complement

1 1 0 0

Step 2: Compute the 1’s complement


Making a negative number in 2’s complement

1 1 0 0

Step 3: Add 1
Making a negative number in 2’s complement

1 1 0 1

Step 3: Add 1
Making a negative number in 2’s complement

1 1 0 1

That’s it! 1101 is 2’s complement for -3


2’s complement in 3 bits

Value
0 0 0 0
0 0 1 1
1 1 1 -1
0 1 0 2
1 1 0 -2
0 1 1 3
1 1 0 -3
1 0 0 -4
2’s complement in 3 bits

Value
0 0 0 0
0 0 1 1
1 1 1 -1
0 1 0 2
1 1 0 -2
0 1 1 3
1 1 0 -3
1 0 0 -4

I We no longer have a negative zero


I We gained an extra negative number (-4 does not have a
negative!)
I Our range is now -4–3 (from (−2n ) to (2n − 1) for n bits)
Addition

I 2’s complement has a really cool property with addition and


subtraction
I It works exactly the same way as unsigned (before we learned
about negatives) binary addition!
Example addition (8 + -5)

0 1 1 1 7
+ 0 1 0 1 5
=

We want to add 7 + (-5) to get an answer of 2. First we start with


7 and 5.
Example addition (8 + -5)

0 1 1 1 7
+ 0 1 0 1 5
=

We convert the 5 to -5 in 2’s complement.


Example addition (8 + -5)

0 1 1 1 7
+ 1 0 1 0 5
=

We convert the 5 to -5 in 2’s complement.


Example addition (8 + -5)

0 1 1 1 7
+ 1 0 1 1 -5
=

We convert the 5 to -5 in 2’s complement.


Example addition (8 + -5)

0 1 1 1 7
+ 1 0 1 1 -5
=

We perform regular binary addition.


Example addition (8 + -5)

0 1 1 1 7
+ 1 0 1 1 -5
= 0

We perform regular binary addition.


Example addition (8 + -5)

1 1

0 1 1 1 7
+ 1 0 1 1 -5
= 1 0

We perform regular binary addition.


Example addition (8 + -5)

1 1 1

0 1 1 1 7
+ 1 0 1 1 -5
= 0 1 0

We perform regular binary addition.


Example addition (8 + -5)

1 1 1 1

0 1 1 1 7
+ 1 0 1 1 -5
= 0 0 1 0 2

We perform regular binary addition.


Example addition (8 + -5)

1 1 1 1

0 1 1 1 7
+ 1 0 1 1 -5
= 0 0 1 0 2

Like magic, we have the correct answer of 2!


How to do 2’s complement

I To convert from positive to negative, flip all the bits and add
1!
I To convert from negative to positive, flip all the bits and add
1!
I “Flip all the bits” is also called “1’s complement”
I For n bits, we can represent all integers from −2n to 2n − 1
How to do 2’s complement

I To convert from positive to negative, flip all the bits and add
1!
I To convert from negative to positive, flip all the bits and add
1!
I “Flip all the bits” is also called “1’s complement”
I For n bits, we can represent all integers from −2n to 2n − 1
I Addition is very easy. Adding two positive numbers, two
negative numbers, one positive and one negative, etc., all
works the same!

You might also like