AlbaCode
Higher / Computer Systems / Two's Compliment

Two's Compliment

Learn how computers store negative numbers.

Introduction

In National 5, we learned that all data within a computer is represented using a system called binary.

The binary system that we learned in National 5 has 1 major flaw! It can't represent negative numbers!

How do we store -1 in binary?

For this we use a system called two's compliment.

Two's Compliment

At National 5 level we learned that we use place values to denote the value of each binary digit.

1286432168421
        

At Higher, we use the two's compliment system.

In this system, the leftmost place value becomes a negative number.

For example, our place values for an 8-bit binary number would be as follows:

-1286432168421
        

At Higher, we are not limited to 8-bit numbers and therefore the negative number might change! However, it is always the leftmost place value!

For example,

4 bits

-8421
    

6 bits

-32168421
      

8 bits

-1286432168421
        

12 bits

-204810245122561286432168421
            

etc...

Binary to Denary

We can convert any two's compliment binary number into its decimal equivalent.

For example,

Convert the two's compliment binary number 1001 1101 to denary.


We start by writing our place values. Remember, the leftmost place value is always a negative number!

-1286432168421
        

We then add the binary number we are trying to convert into the table.

-1286432168421
10011101

Much like in National 5, we can simply add up the numbers above the 1's

-128 + 16 + 8 + 4 + 1 = -99

Therefore:

1001 1101 -> -99

Denary to Binary

If we wish to take a denary number and convert it to two's compliment binary we can follow one of two methods.

Method 1 is similar to one of the methods used in National 5, we simply look for the numbers that add together to give us our target number and add 1's below them, putting zero's everywhere else.

Method 1 can be tricky to do as we are using negative numbers. Method 2 will take slightly longer but is easier once you learn the method!

Denary to Binary - Method 1

For example, convert the number -19 to an 8-bit two's compliment binary number

We first start by writing our place values.

-1286432168421
        

We then look at our place values and try to find numbers which add to give use our target number, in this case -19. You can only use each number once!

We can see that -128, 64, 32, 8, 4 and 1 add to give us -19. We put 1's under these numbers.

-1286432168421
111 1  1

We can then put 0's under all other numbers.

-1286432168421
11101001

We can then put 0's under all other numbers.

Therefore:

19 -> 0001 0011

Denary to Binary - Method 2

A lot of people find method 1 quite difficult because of the negative number used in the calculation. Instead we can use method 2!

For example, if we wanted to convert the denary number -25 to binary, it would be quite difficult to intuatively find numbers that add up to give -25.

For example, if we wanted to convert the denary number -25 to binary, it would be quite difficult to intuatively find numbers that add up to give -25.

In this case, method 2 should be used. This method will work with any two's compliment denary to binary conversion.

Method

This method involves 3 steps:

  1. Write the positive representation of the number
  2. Flip the bits
  3. Add 1

Example

Convert the denary number -25 to an 8-bit two's compliment binary number.

We first begin by writing the binary place values.

-1286432168421
        

Step 1

We start by writing the the positive representation of the number. In our case this is 25.

-1286432168421
00011001

Step 2

We then do what is known as 'flipping the bits'. This means we turn all of the 0's to 1's, and all the 1's to 0's.

Our current number is:

-1286432168421
00011001

If we flip the bits, this becomes:

-1286432168421
11100110

Notice that all of the 0's have become 1's, and all of the 1's have become 0's.

Step 3

Our 3rd step is to add 1. This is where we have to do a little bit of maths in binary.

If you know how to add in binary, you can skip to Step 3 - Continued.

How to do Maths in Binary

Binary maths works much like normal maths that you use everyday!

Let's take 0 + 0, in binary this is still the same.

0 + 0 = 0

What about 0 + 1? Well this is the same too!

0 + 1 = 1

What about 1 + 1? Well this would usually be 2, but 2 isn't written with a 2 in binary, it is written as 10!

1 + 1 = 10

21
10

Let's try one more, what about 11 + 1 in binary(3 + 1 in denary)?

11 + 1 = 100

421
100

We all know 3 + 1 is 4 (or 11 + 1 is 100 if written in binary), but we need to understand why!

Lets's think in denary for a minute. When we do 9 add 1, we write a 0 and carry the one to the column to the left resulting in 10.

We do the same in binary! If we do 1 + 1 it results in a 0 and the 1 is carried to the column to the left.

If there is also a 1 in this column, we will have to write a 0 and carry a 1 to the column to the left again.

For Example, lets do 0111 + 1:

8421
0111
   1
    

1 + 1 is 0 (carry 1, so we write 0 and try to add 1 to the next column

8421
0111
  1 
   0

1 + 1 is 0 (carry 1, so we write 0 and try to add 1 to the next column

8421
0111
 1  
  00

Again, 1 + 1 is 0 (carry 1, so we write 0 and try to add 1 to the next column

8421
0111
1   
 000

This time we have 0 + 1, we know that this is simply 1.

8421
0111
    
1000

Step 3 - Continued

So far we have:

  • Written the positive representation of the number
  • Flipped the bits

Our current number is:

-1286432168421
11100110

we now have to add 1.

1286432168421
11100110
       +1
        

This gives us:

1286432168421
11100110
       +1
11100111

Therefore, our final answer is:

-25 -> 1110 0111

Denary to Binary - Method 2 (Another Example)

Convert the denary number -32 to two's compliment binary.

We will follow the 3 steps:

  1. Write the positive representation of the number
  2. Flip the bits
  3. Add 1

Step 1

We start by writing the the positive representation of the number. In our case this is 32.

-1286432168421
00100000

Step 2

We then 'flip the bits'. This means we turn all of the 0's to 1's, and all the 1's to 0's.

Our current number is:

-1286432168421
00100000

If we flip the bits, this becomes:

-1286432168421
11011111

Again, notice that all of the 0's have become 1's, and all of the 1's have become 0's.

Step 3

Our last step is to add 1.

1286432168421
11011111
       +1
        

This time we will have to carry the 1 as we add it to each column.:

This gives us:

1286432168421
11100110
       +1
11100000

Therefore, our final answer is:

-32 -> 1110 0000