# How to compute two's complement of a negative number?

I made the search on here and on google and couldn't find anything that answered the topic title.

From my bit of understanding, two's complement can be used to make a decimal number, negative.

Which is to say, computing the two's complement of 5 decimal would result in -5 decimal and can be represented as -101 binary, 111111011 binary, or 507 decimal.

Would the two's complement of -5 decimal just result in either of these representations? (-111111011 binary, -507 decimal, or 101 binary)

Edit: I started out trying bitwise not operations and decided to write a function for two's complement also. I'm uncertain of the results for larger numbers. Example: twosComplement(-5898238923873); == { decimal: "589821056097" negativeBinary: "-111110111011010101011111011001110101110011111" negativeDecimal: "-34594551032735" twosCompliment: "1000100101010100000100110001010001100001" }

Two's compliment is just associating all numbers $\pmod {2^n}$ with the same bit pattern.

So if you are on an $8$ bit machine, then $-5$ and $-5 + 2^8 = 251$ will have the same bit pattern. The bit swapping tricks are just a shortcut, but it's not too hard to work out what they would be.

To work out the bit pattern for $-z$ (in your case $z=5$), you want to find the binary representation of $2^n - z$. A shortcut could be to find the bit pattern of $(2^n - 1) - z$, then add $1$:

$$\begin{array} {c|cccc} 2^4 - 1 & 1111 \\ 5 & 0101 \\ \hline \text{subtract} & 1010 \\ \text{add 1} & 0001 \\ \hline -5 & 1011 \end{array}$$

The subtraction step is just toggling bits, there is never a carry. The process is the same as going from a positive to a negative number: just reverse the bits and add 1.

• I'll implement this method. Soon after I write my own power() and root() functions :) – ŽaMan Nov 8 '15 at 17:53

To make sense of the phrase "two's complement" of a number, it really needs to be given in binary, with some fixed number of bits, not in decimal. For $-5$ decimal, you already saw that in binary this is equal to the two's complement of $101$, which gave you $11111011$ (actually you chose to use $9$ bits instead of $8$ bits, for some reason).

To find the two's complement of any number that is given in binary, you simply flip all the bits and add 1.

In the case of $-5$ decimal, first convert to binary to get $11111011$, then flip the bits to get $00000100$, then add $1$ to get $00000101$.

• The reason for the 9 bits was because I came across this: stackoverflow.com/questions/9172115/… ; regarding the final result of 00000101 would it be required or helpful, for other techniques which might use the result, to keep the zero padding? – ŽaMan Nov 8 '15 at 17:14
• You need at least one zero in there if you're using the two's complement representation, because otherwise the correct two's complement interpretation of the number will be as a negative number - $101$ in two's complement means $-3$ decimal, not $5$ decimal. – Dustan Levenstein Nov 8 '15 at 17:22
• Computers typically use a fixed number of bits, which is why I stuck with $8$ bits here. Actually, computers use a fixed number of bytes, where a byte is 8 bits, so that's why I remarked that $9$ bits was a strange choice. – Dustan Levenstein Nov 8 '15 at 17:23