title=Fundamentals of Data Representation: Two's complement

 ← Binary arithmetic Two's complement Status register →

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

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)