# 2s complement numbers

Introduction
To understand floating-point numbers, you must understand two basic concepts. These are

• 2s complement binary representation of denary numbers.
• Fixed-point binary representation.

We are going to assume that you understand the binary representation of numbers, and have done some work before in the AS year on 2s complement. We will begin by reminding you of 2s complement binary representation of denary numbers and then look at fixed-point binary representation. When we have done this, we will look at floating-point representation.

2s complement representation
Suppose we want to represent using 2s complement both positive and negative numbers using just five bits. We would weight each bit thus: The biggest number we could represent is +15 (01111) and the smallest is -16 (10000).

Suppose we want to represent both positive and negative numbers using 2s complement with just 4 bits. We would weight each bit thus: The biggest number that could be represented is now +7 (0111) whilst the smallest number is -8 (1000).

Notice that however many bits we are using to represent 2s complement numbers; the bit on the left is negative whilst all the other bits are positive. We will use five bits to represent 2s complement numbers in the following examples.

2s complement representation of positive numbers using 5 bits

5 is represented as 00101
9 is represented as 01001
14 is represented as 01110 2s complement representation of negative numbers using 5 bits

-5 is represented as 11011 (This means -16 + (8 + 2 + 1) = -16 + 11 = -5)
-9 is represented as 10111 (This means -16 + (4 + 2 + 1) = -16 + 7 = -9)
-1 is represented as 11111 (This means -16 + (8 + 4 + 2 + 1) = -16 + 15 = -1) It is a little harder working with negative numbers in 2s complement form but luckily there is a trick you can use to convert really quickly. You need to do the following to convert a negative denary number into a 2s complement binary number.

1. Convert the positive version of the number into binary.
2. Copy the bits from the right hand side of this binary number up to and including the first ‘one’ bit.
3. Invert the remaining bits.

Example 1. Convert -9 into a 2s complement binary number.

1. +9 is 01001
2. Starting with the right most bit, copy all the bits up to and including the first 1 bit. This actually gives us 1 in this case.
3. Invert the remaining bits. This now gives us 10111.

If you do the sums, you will see that this is -16 + (4 + 2 +1) = -16 + 7 = -9

Example 2. Convert -2 into a 2s complement binary number.

1. +2 is 00010
2. Starting with the right most bit, copy all the bits up to and including the first 1 bit. This gives us 10 in this case.
3. Invert the remaining bits. This now gives us 11110.

If you do the sums, you will see that this is -16 + (8 + 4 +2) = -16 + 14 = -2

Example 3. Convert -8 into a 2s complement binary number.

1. +8 is 01000
2. with the right most bit, copy all the bits up to and including the first 1 bit. This gives us 1000 in this case.
3. c) Invert the remaining bits. This now gives us 11000

If you do the sums, you will see that this is -16 + 8 = -8 Getting back to a negative denary number.
If you know a number is in 2s complement form, it is straightforward getting back to a negative denary number.

1. Starting with the right most bit, copy all the bits up to and including the first ‘1’ bit.
2. Invert the remaining bits. You now have a negative binary number that is not in 2s complement form.
3. Convert using the normal weighting method.

You always know if a number is negative because the left hand bit is a ‘one’. If it is a ‘zero’ then know it is positive.

Example 4. Convert 10100 to a denary number.

1. Starting with the right most bit, copying all the bits up to and including the first ‘1’ bit gives us 100.
2. Inverting the remaining bits gives us -(01100).
3. Converting gives us -(8 + 4) = -12.

Example 5. Convert 10110 to a denary number.

1. Starting with the right most bit, copying all the bits up to and including the first ‘1’ bit gives us 10.
2. Inverting the remaining bits gives us -(01010).
3. Converting gives us -(8 + 2) = -10.

Example 6. Convert 10011 to a denary number.

1. Starting with the right most bit, copying all the bits up to and including the first ‘1’ bit gives us 1.
2. Inverting the remaining bits gives us -(01101).
3. Converting gives us -(8 + 4 + 1) = -13.

Converting a positive 2s complement number involves simply adding the weighting value of each bit together. Remember, you know if a number is negative or positive by examining the left hand bit. If it is one, it is negative. If it is zero then it is positive. 