title=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:
-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.
Signed binary numbers If the MSB is 0 then the number is positive, if 1 then the number is negative. 0000 0101 (positive) 1111 1011 (negative) |
Method: Converting a Negative Denary Number into Binary Twos Complement 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[edit]
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.
Method 1: converting twos complement to denary 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!) |
Method 2: converting twos complement to denary 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?
Method 1: converting twos complement to denary 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!) |
Method 2: converting twos complement to denary 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...
Method 1: converting twos complement to binary 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) |
Method 2: converting twos complement to binary 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) |
Exercise: two's complement numbers Convert the following two's complement numbers into denary: 0001 1011 Answer: (positive number) 27
1111 1111 Answer: (negative number) 0000 0001 = -1
0111 1101 Answer: (positive number) 125
1001 1001 Answer: (negative number) 0110 0111 = -103
1011 1000 Answer: (negative number) 0100 1000 = -72
81 (hexadecimal) Answer: (using 4 bits for each HEX char) 1000 0001
(negative number) -> 0111 1111 = -127 A8 (hexadecimal) Answer: (using 4 bits for each HEX char) 1010 1000
(negative number) -> 0101 1000 = -88 Convert the following numbers into negative numbers written in binary 0000 0001 Answer: 1111 1111
0110 0000 Answer: 1010 0000
0111 1111 Answer: 1000 0001
12 (denary) Answer: 0000 1100 = +12 -> 1111 0100 = -12
67 (denary) Answer: 0100 0011 = +67 -> 1011 1101 = -67
34 Answer: 0010 0010 = +34 -> 1101 1110 = -34
34 (hexadecimal) Answer: (using 4 bits for each HEX char) 0011 0100 = +52 -> 1100 1100 = -54
7E (hexadecimal) Answer: (using 4 bits for each HEX char) 0111 1110 = +126 -> 1000 0010 = -126 |
Binary Subtraction[edit]
Example: 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 − 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 |
Exercise: Binary subtraction Find the answers to the following sums in binary, show your working 0110 1100 (108) - 0000 0111 (7) Answer: 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) Answer: 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) Answer: 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) Answer: Convert the HEX values to binary 0010 0011 + 1110 0001 0000 0100 = 4 (¹)¹¹¹ ¹¹ the one carried over the bit 9 is ignored 0001 0010 (10) - 1110 0001 (-31) Answer: They have tried to trick you. What is a negative number minus a negative number? X - (-Y) = X + Y 0001 0010 (10) + 0001 1111 (31) 0011 0001 = 49 (¹) ¹¹ ¹¹ the one carried over the bit 9 is ignored |