Two's Complement

Course Content Specification

Two's Complement Notation

So far we have looked how to store positive integers, but we need to be able to also store negative integers. The method we will look at to store negative integers is called two’s complement.


Creating a Two's complement number


Tutorial Video

Negative Integers.mp4

When a binary number is represented in 2’s complement the MSB (Most Significant Bit) which is the left most bit functions as a sign bit.

Examples:

1001 1010 = -102

0111 1110 = +126

Worked Example 1  - Negative 25

Worked Example 2 - Negative 36

Converting a Two's Complement number to decimal

If you are given a negative number which is already in two’s complement and you need to find out what value it is simply carry out the same conversion steps again.

Example:

1111 0111(-9)   (in this case we know that the answer is 0000 1001 = 9 what we aim to get)

Another Method of Converting

If we want -10 in 8 bit two’s complement find the positive version: 0000 1010

Range of Representation

Highest Numbers

The maximum positive number that can be represented when using twos complement is when the MSB ( the left hand bit) is 0 and all remaining bits are 1.

E.g. 8 bit = 0111 1111 (+127)

16 bit = 0111 1111 1111 1111 (+32,767)

Lowest Numbers

The lowest negative number that can be represented when using twos complement is when the MSB is 1 and all remaining bits are 0

8 bit = 1000 0000 (-128)

16 bit = 1000 0000 0000 0000 (-32,768)

Place values in Two's Complement

It is exactly the same as earlier except the most significant bit is a negative value. If we look at an 8 bit two’s complement number such as 1111 0111 (-9)

You should notice that the right hand bit is (-27) which is (-2n-1) where n is the number of bits.

And -9 = -128 + 64 + 32 +16 + 4 + 2 + 1

So the range is from -2n-1 to + (2n-1) -1

where n is the number of bits that you are using (usually 8 or 16 in exams)

How do you know if a number is negative?

We don’t!

Data in memory is just data, it depends on the instructions used by the processor (influenced by things such as data types etc) to control how data is manipulated.

1111 0111 could be -9 or 247, or a pixel in a bitmap.

But different instructions would control whether the processor interpreted it as a signed number, unsigned number or a pixel in a bitmap. This will control how things such as overflow and carry operations are carried out. The same as if you are lifting a wardrobe and a cup you would have to ‘lift’ each one differently.

What about in exams?

In exams if you are looking at a number in two's complement notation remember that if the left hand bit is a 1 it is a negative number and if it is 0 it is positive.

Test Yourself

Convert the number -54 using 8 bit two's Complement notation

11001010

Convert the number -104 using 8 bit two's Complement notation

10011000