What is “2's Complement”?

前端 未结 23 2529
名媛妹妹
名媛妹妹 2020-11-21 05:59

I\'m in a computer systems course and have been struggling, in part, with Two\'s Complement. I want to understand it but everything I\'ve read hasn\'t brought the p

23条回答
  •  醉酒成梦
    2020-11-21 06:25

    Looking at the two's complement system from a math point of view it really makes sense. In ten's complement, the idea is to essentially 'isolate' the difference.

    Example: 63 - 24 = x

    We add the complement of 24 which is really just (100 - 24). So really, all we are doing is adding 100 on both sides of the equation.

    Now the equation is: 100 + 63 - 24 = x + 100, that is why we remove the 100 (or 10 or 1000 or whatever).

    Due to the inconvenient situation of having to subtract one number from a long chain of zeroes, we use a 'diminished radix complement' system, in the decimal system, nine's complement.

    When we are presented with a number subtracted from a big chain of nines, we just need to reverse the numbers.

    Example: 99999 - 03275 = 96724

    That is the reason, after nine's complement, we add 1. As you probably know from childhood math, 9 becomes 10 by 'stealing' 1. So basically it's just ten's complement that takes 1 from the difference.

    In Binary, two's complement is equatable to ten's complement, while one's complement to nine's complement. The primary difference is that instead of trying to isolate the difference with powers of ten (adding 10, 100, etc. into the equation) we are trying to isolate the difference with powers of two.

    It is for this reason that we invert the bits. Just like how our minuend is a chain of nines in decimal, our minuend is a chain of ones in binary.

    Example: 111111 - 101001 = 010110

    Because chains of ones are 1 below a nice power of two, they 'steal' 1 from the difference like nine's do in decimal.

    When we are using negative binary number's, we are really just saying:

    0000 - 0101 = x

    1111 - 0101 = 1010

    1111 + 0000 - 0101 = x + 1111

    In order to 'isolate' x, we need to add 1 because 1111 is one away from 10000 and we remove the leading 1 because we just added it to the original difference.

    1111 + 1 + 0000 - 0101 = x + 1111 + 1

    10000 + 0000 - 0101 = x + 10000

    Just remove 10000 from both sides to get x, it's basic algebra.

提交回复
热议问题