# Connexions

You are here: Home » Content » 2's Complement Tutorial

### Recently Viewed

This feature requires Javascript to be enabled.

# 2's Complement Tutorial

Module by: UIUC ECE 290 AD7-Team Vague. E-mail the author

Summary: A tutorial for 2's complement: what it is, how to code numbers and addition and subtraction of 2's complement number.

2’s Complement

There are three methods that we commonly use to represent numbers in binary: Unsigned binary , 1’s Complement, and 2’s Complement. We’re going to focus on 2’s complement because this is the most effective way of adding and subtracting negative numbers. The range of 2’s complement is -2n-1 to 2n-1 - 1 (all 2n bit patterns).

Converting:

From base10 to 2’s Complement:

Positive: all positive numbers in 2’s complement are the same as they would be in unsigned binary

Negative: there are three steps to follow, for ALL conversions to negatives

1. Find the binary representation of the absolute value of the number that you want to convert
2. Complement all the bits (“flip them”)

Example: how to convert -63 into 2’s complement

i）Decimal to binary

 Magnitude: 63 quotient remainder 63/2 31 1 31/2 15 1 15/2 7 1 7/2 3 1 3/2 1 1 1 N/A 1

take all the remainder’s : 111111

extend to 8 bits: 00111111

ii) Binary to 1’s complement:flip 0s and 1s

11000000

iii) 1’s complement to 2’s complement:add 1 to the least significant bit

11000001

iii) Check value

(11000001)2’= (-1) * 27 + (1) * 26 + (1) * 20 = -63

From 2’s Complement to base10:

Positive: Treat it as an unsigned binary number and convert normally.

Negative: We have two methods that are commonly used

1. If there’s a lot of weighting involved, it’s just as simple to go backwards from the beginning
• Original number- 10011
• We had to add 1 to it originally, so look at that.

00001

10011

• Now, what binary number would make the 2’s C we end up with

10010

00001

10011

Just flip the bits back

01101

and you have 8 + 4 + 1 = 13, the the number is -13

1. If we have a small number of bits, “Negative Weighting” works
1. The left most bit with 1 has weight -2n , and every other bit after that has its positive weight as usual in binary
2. 100011 = -32 + 2 + 1
3. 100000111 = -128 + 4 + 2 + 1
2. Formal definition: negative numbers in n-bit 2’s complement are obtained by subtracting from a large power of 2; specifically, 2n. Observe that complementing all the bits is the same operation as subtracting from a bit pattern of all 1’s and that:

So subtracting from 2n is equivalent to subtracting from 2n - 1 (complementing all the bits) and then adding 1.

1. The easiest possible way to take the negative of a number is to find the rightmost 1 in the binary representation and complementing all the bits to the left of it.

Some terminology:

• The carry refers to the binary number formed by all the carries in the addition.
• The carry-out is the final bit of the carry, which is always discarded as it is beyond the nth bit of the sum.
• Overflow is when the sum of the numbers cannot be represented within n-bits. When this occurs, the addition process produces an incorrect sum.

Adding 2’s complement numbers is just like adding any two binary numbers. You don’t have to worry about the signs of the summands or the sum.

00111 < carry

00111

+ 00011

01010 < sum

With 2’s complement, you do have to worry about overflow. This can be checked by looking at the two most significant bits of the carry. If those two bits are different, overflow has occurred.

10110 < overflow, since two most significant bits are different

10110

+ 10011

(1)01001 < the 6th bit is truncated, since we are only adding 5 bits

11110 00111

10110 00011

+ 01011 + 00101

(1)01001 (0)01000

In the example on the left, there is no overflow, even though there is a carry-out that is discarded, because the 2 most significant bits of the carry are the same. Since we’re adding two 5 bit numbers, the sum is just truncated to 5 bits, so 10110 + 01011 = 01001.

Subtraction in 2’s Complement

1)

1 1 1 1 1 1 1 1

- 1 1 0 0 0 0 0 1

Original problem

2)

1 1 1 1 1 1 1 1

+ -(1 1 0 0 0 0 0 1)

Subtracting a number is the same as adding the negative of that number

3)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

Convert the second number into its negative value

4)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

_ _ _ _ _ _ _ 0

Add the least significant bits. Note that there is a carry bit that will be added to the next set of bits.

5)

1 1 1 1 1 1 11

+ 0 0 1 1 1 1 1 1

_ _ _ _ _ _ 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

6)

1 1 1 1 1 11 1

+ 0 0 1 1 1 1 1 1

_ _ _ _ _ 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

7)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

_ _ _ _ 1 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

8)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

_ _ _ 1 1 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

9)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

_ _ 1 1 1 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

10)

1 1 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

_ 0 1 1 1 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced for the next set of bits.

11)

11 1 1 1 1 1 1

+ 0 0 1 1 1 1 1 1

0 0 1 1 1 1 1 0

Add the next bits (going to the left). Don’t forget the carry bit that was produced by the addition of the previous bits. Also note that a carry bit was produced. The carry bit has no more bits to be added to. Now we need to check for overflow.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks