A-level Computing/AQA/Paper 2/Fundamentals of data representation/Twos 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:
| -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| MSB | LSB | ||||||
| 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 |
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.
Template:CPTExample If the MSB is 0 then the number is positive, if 1 then the number is negative.
0000 0101 (positive) 1111 1011 (negative)
Template:ExampleRobox 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 Template:Robox/Close
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.
Template:CPTExample 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:
1111 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 = −5 (remember the sign you worked out earlier!)
Template:ExampleRobox 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
1111 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? Template:ExampleRobox
1111 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 = −4 (remember the sign you worked out earlier!)
Template:ExampleRobox 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
1111 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...
Template:CPTExample 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 (−5)
Template:CPTExample 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 (−5)
Template:CPTExercise Template:CPTQuestion Template:CPTAnswerTab(positive number) 27 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(negative number) 0000 0001 = -1 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(positive number) 125 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(negative number) 0110 0111 = -103 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(negative number) 0100 1000 = -72 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(using 4 bits for each HEX char) 1000 0001 (negative number) -> 0111 1111 = -127 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(using 4 bits for each HEX char) 1010 1000 (negative number) -> 0101 1000 = -88 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab1111 1111 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab1010 0000 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab1000 0001 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab0000 1100 = +12 -> 1111 0100 = -12 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab0100 0011 = +67 -> 1011 1101 = -67 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab0010 0010 = +34 -> 1101 1110 = -34 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab(using 4 bits for each HEX char) 0011 0100 = +52 -> 1100 1100 = -54 Template:CPTAnswerTabEnd Template:CPTQuestion Template:CPTAnswerTab (using 4 bits for each HEX char) 0111 1110 = +126 -> 1000 0010 = -126 Template:CPTAnswerTabEnd Template:Robox/Close
Range of two's complement values
If the msb is being used to provide negative values it follows that the maximum possible value will be limited. The number of possible values remains the same and the range of these numbers will include negative values as well as the positive values.
Range of binary digits using two's complement representation:
| Rule: | ||
|
Minimum value = For example, for 3 digits: |
||
| Rule: | ||
|
Maximum value = For example, for 3 digits: |
||
Binary Subtraction
Template:ExampleRobox 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 −
An easier way to subtract Y from X is to add the negative value of Y to the value of X
X−Y = X+(−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(−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
Template:CPTExercise Template:CPTQuestionTab Find the answers to the following sums in binary, show your working
0110 1100 (108) - 0000 0111 (7)
Template:CPTQuestionTabEnd Template:CPTAnswerTab 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
Template:CPTAnswerTabEnd Template:CPTQuestionTab
0001 1111 (31) - 0001 0011 (19)
Template:CPTQuestionTabEnd Template:CPTAnswerTab 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
Template:CPTAnswerTabEnd Template:CPTQuestionTab
0111 0111 (119) - 0101 1011 (91)
Template:CPTQuestionTabEnd Template:CPTAnswerTab 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
Template:CPTAnswerTabEnd Template:CPTQuestionTab
23 (hex) - 1F (hex)
Template:CPTQuestionTabEnd
Template:CPTAnswerTab
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
Template:CPTAnswerTabEnd Template:CPTQuestionTab
0001 0010 (10) - 1110 0001 (-31)
Template:CPTQuestionTabEnd
Template:CPTAnswerTab
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:
0001 0010 (10) + 0001 1111 (31) 0011 0001 = 49 (¹) ¹¹ ¹¹ the one carried over the bit 9 is ignored
Template:CPTAnswerTabEnd Template:Robox/Close Template:BookCat