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"
}
 A: 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$.
A: 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.
