Reputation: 1
I read the wiki article and many other articles. The definition in wiki is that "The defining property of being a complement to a number with respect to 2N is simply that the summation of this number with the original produce 2N. " The formula to compute the original value using the two's completment representaion is that "a two's-complement number system encodes positive and negative numbers in a binary number representation. The weight of each bit is a power of two, except for the most significant bit, whose weight is the negative of the corresponding power of two."
For example, for a number p=-3 using binary with numbers up to three-bits , to calculate the two's complement representation we use 1000-011=101, which is the two's complement representation of p, -1x2^2+0x2^1+1x2^0=-3 is the original value of p. I just don't know why we can get the original value by applying the formula on the two's completment representation? Is there any reasoning or proof process? Or it is just another definition and assignment rules such that we use the formula to assign each original value to corresponding two's completment representation.
Upvotes: 0
Views: 74
Reputation: 11332
Twos complement representation is the same as standard base-two representation, except the high bit is negative its normal value. So, for example 8-bit twos-complement has bits whose values are
-128 64 32 16 8 4 2 1
So given 10001001, its value is -128 + 8 + 1 = -117
Answering your later question. There are multiple definitions of "two's complement", all of them equivalent, so I'm not really sure which one you're considering to be the "official" one.
For simplicity, I'm going to assume an 8-bit computer, so just that the numbers are small.
Note that the ADD instruction on your computer doesn't really perform an addition. It performs an addition, mod 256. If you did 240 + 27, you'd get 11, and you wouldn't be the least bit surprised by this. That's addition on a computer.
But note that mod 256, 240 and -16 are the same number. So the computer has no idea if you are adding 240 and 27, or -16 and 27, and in both cases, you get the same answer, 11.
When dealing with unsigned numbers, you are telling the computer to treat the 8 bits as a number between 0 and 255. When dealing with signed numbers, you, not the computer, are treating numbers bigger than 128 as if they were 256 smaller. But the computer doesn't care.
Note that the exact same thing that works with ADD also works with SUB and MUL. It doesn't work with division.
I hope this clears up what's going on. There's no magic.
Upvotes: 0