A-level Computing/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Data Representation/Two's complement



 Nearly all computers work purely in binary. That means that they only use ones and zeros, and there's no - or + symbol that the computer can use. The computer must represent negative numbers in a different way.

We can represent a negative number in binary by making the most significant bit (MSB) a sign bit, which will tell us whether the number is positive or negative. The column headings for an 8 bit number will look like this:

Here, the most significant bit is negative, and the other bits are positive. You start with -128, and add the other bits as normal. The example above is -67 in denary because: (-128 + 32 + 16 + 8 + 4 + 1 = -67)

-1 in binary is 11111111.

Note that you only use the most significant bit as a sign bit if the number is specified as signed. If the number is unsigned, then the msb is positive regardless of whether it is a one or not.

If the MSB is 0 then the number is positive, if 1 then the number is negative.

0 000 0101 (positive) 1 111 1011 (negative)

Let's say you want to convert -35 into Binary Twos Complement. First, find the binary equivalent of 35 (the positive version)

32 16   8   4   2   1   1   0   0   0   1   1

Now add an extra bit before the MSB, make it a zero, which gives you:

64 32 16   8   4   2   1   0  1   0   0   0   1   1

Now 'flip' all the bits: if it's a 0, make it a 1; if it's a 1, make it a 0:

64 32 16   8   4   2   1   1  0   1   1   1   0   0

This new bit represents -64 (minus 64). Now add 1:

64 32 16   8   4   2   1   1  0   1   1   1   0   0                       + 1  1  0   1   1   1   0   1

If we perform a quick binary -> denary conversion, we have: -64 + 16 + 8 + 4 + 1 = -64 + 29 = -35

Converting Negative Numbers
To find out the value of a twos complement number we must first make note of its sign bit (the most significant, left most bit), if the bit is a zero we work out the number as usual, if it's a one we are dealing with a negative number and need to find out its value.

To find the value of the negative number we must find and keep the right most 1 and all bits to its right, and then flip everything to its left. Here is an example:

1 111 1011 note the number is negative

1111 1011 find the right most one 1111 1011 0000 0101 flip all the bits to its left

We can now work out the value of this new number which is:

128 64  32  16   8   4   2   1    0   0   0   0   0   1   0   1                       4   +   1 = &minus;5   (remember the sign you worked out earlier!)

To find the value of the negative number we must take the MSB and apply a negative value to it. Then we can add all the heading values together

1 111 1011 note the number is negative -128 64  32  16   8   4   2   1     1   1   1   1   1   0   1   1 -128 +64 +32 +16  +8      +2  +1 = -5

How about a more complex example?

1 111 1100 note the number is negative 1111 1100 find the right most one 1111 1100 0000 0100 flip all the bits to its left

128 64  32  16   8   4   2   1    0   0   0   0   0   1   0   0                       4         = &minus;4   (remember the sign you worked out earlier!)

To find the value of the negative number we must take the MSB and apply a negative value to it. Then we can add all the heading values together

1 111 1100 note the number is negative

-128 64  32  16   8   4   2   1     1   1   1   1   1   1   0   0 -128 +64 +32 +16  +8  +4          = -4

So we know how to work out the value of a negative number that has been given to us. How do we go about working out the negative version of a positive number? Like this, that's how...

Take the binary version of the positive number

0000 0101 (5) 0000 0101 find the right most one 0000 0101 1111 1011 flip all the bits to its left

So now we can see the difference between a positive and a negative number

0000 0101 (5) 1111 1011 (&minus;5)

Take the binary version of the positive number

starting with -128, we know the MSB is worth -128. We need to work back from this:

-128 64  32  16   8   4   2   1     1   1   1   1   1   0   1   0    -128 +64 +32 +16  +8      +1      = -5

0000 0101 (5) 1111 1011 (&minus;5)

(positive number) 27

(negative number) 0000 0001 = -1

(positive number) 125

(negative number) 0110 0111 = -103

(negative number) 0100 1000 = -72

(using 4 bits for each HEX char) 1000 0001 (negative number) -> 0111 1111 = -127

(using 4 bits for each HEX char) 1010 1000 (negative number) -> 0101 1000 = -88

1111 1111

1010 0000

1000 0001

0000 1100 = +12 ->  1111 0100 = -12

0100 0011 = +67 ->  1011 1101 = -67

0010 0010 = +34 ->  1101 1110 = -34

(using 4 bits for each HEX char) 0011 0100 = +52 ->  1100 1100 = -52

(using 4 bits for each HEX char) 0111 1110 = +126 ->  1000 0010 = -126

Binary Subtraction
When it comes to subtracting one number from another in binary things can get very messy. X (82 denary) 0101 0010 Y (78 denary) 0100 1110 &minus;

An easier way to subtract Y from X is to add the negative value of Y to the value of X X&minus;Y = X+(&minus;Y)

To do this we first need to find the negative value of Y (78 denary)

0100 1110 find the right most one 0100 1110 1011 0010 flip all the bits to its left

Now try the sum again 0101 0010    X( 82 denary) 1011 0010 +  Y(&minus;78 denary) 0000 0100 (¹)¹¹¹  ¹       the one carried over the bit 9 is ignored

Which comes out as:

128 64  32  16   8   4   2   1    0   0   0   0   0   1   0   0                       4         = 4 = 82-78

Find the answers to the following sums in binary, show your working 0110 1100 (108) - 0000 0111 (7)

Convert the 0000 0111 into a negative number 1111 1001 = -7 Add both numbers together: 0110 1100 + 1111 1001    0110 0101 = 101 (¹)¹¹¹¹         the one carried over the bit 9 is ignored

0001 1111 (31) - 0001 0011 (19)

Convert the 0001 0011 into a negative number 1110 1101 = -19 Add both numbers together: 0001 1111 + 1110 1101    0000 1100 = 12 (¹)¹¹¹¹ ¹¹¹    the one carried over the bit 9 is ignored

0111 0111 (119) - 0101 1011 (91)

Convert the 0101 1011 into a negative number 1010 0101 = -91 Add both numbers together: 0111 0111 + 1010 0101    0001 1100 = 28 (¹)¹¹   ¹¹¹    the one carried over the bit 9 is ignored

23 (hex) - 1F (hex)

Convert the HEX values to binary

0010 0011 = 23 HEX or 35 denary

0001 1111 = 1F HEX or 31 denary

Now let's find the negative value of 1F

1110 0001 = -31

Add both numbers together: 0010 0011 + 1110 0001    0000 0100 = 4 (¹)¹¹¹   ¹¹    the one carried over the bit 9 is ignored

0000 1010 (10) - 1110 0001 (-31)

They have tried to trick you. What is a negative number minus a negative number? X - (-Y) = X + Y

Let's start by finding the value of the bottom number: 1110 0001 -> 0001 1111 = 31

And by working this out we have the positive value (0001 1111) Add both numbers together: 0000 1010 (10) + 0001 1111 (31)    0010 1001 = 41       ¹ ¹¹