What is “2's Complement”?

前端 未结 23 2469
名媛妹妹
名媛妹妹 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

    I read a fantastic explanation on Reddit by jng, using the odometer as an analogy.

    It is a useful convention. The same circuits and logic operations that add / subtract positive numbers in binary still work on both positive and negative numbers if using the convention, that's why it's so useful and omnipresent.

    Imagine the odometer of a car, it rolls around at (say) 99999. If you increment 00000 you get 00001. If you decrement 00000, you get 99999 (due to the roll-around). If you add one back to 99999 it goes back to 00000. So it's useful to decide that 99999 represents -1. Likewise, it is very useful to decide that 99998 represents -2, and so on. You have to stop somewhere, and also by convention, the top half of the numbers are deemed to be negative (50000-99999), and the bottom half positive just stand for themselves (00000-49999). As a result, the top digit being 5-9 means the represented number is negative, and it being 0-4 means the represented is positive - exactly the same as the top bit representing sign in a two's complement binary number.

    Understanding this was hard for me too. Once I got it and went back to re-read the books articles and explanations (there was no internet back then), it turned out a lot of those describing it didn't really understand it. I did write a book teaching assembly language after that (which did sell quite well for 10 years).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 06:26

    Two complement is found out by adding one to 1'st complement of the given number. Lets say we have to find out twos complement of 10101 then find its ones complement, that is, 01010 add 1 to this result, that is, 01010+1=01011, which is the final answer.

    0 讨论(0)
  • 2020-11-21 06:29

    Lets get the answer 10 – 12 in binary form using 8 bits: What we will really do is 10 + (-12)

    We need to get the compliment part of 12 to subtract it from 10. 12 in binary is 00001100. 10 in binary is 00001010.

    To get the compliment part of 12 we just reverse all the bits then add 1. 12 in binary reversed is 11110011. This is also the Inverse code (one's complement). Now we need to add one, which is now 11110100.

    So 11110100 is the compliment of 12! Easy when you think of it this way.

    Now you can solve the above question of 10 - 12 in binary form.

    00001010
    11110100
    -----------------
    11111110  
    
    0 讨论(0)
  • 2020-11-21 06:29

    2's complement of a given number is the no. got by adding 1 with the 1's complement of the no. suppose, we have a binary no.: 10111001101 It's 1's complement is : 01000110010 And it's 2's complement will be : 01000110011

    0 讨论(0)
  • 2020-11-21 06:30

    2's complement is essentially a way of coming up with the additive inverse of a binary number. Ask yourself this: Given a number in binary form (present at a fixed length memory location), what bit pattern, when added to the original number (at the fixed length memory location), would make the result all zeros ? (at the same fixed length memory location). If we could come up with this bit pattern then that bit pattern would be the -ve representation (additive inverse) of the original number; as by definition adding a number to its additive inverse always results in zero. Example: take 5 which is 101 present inside a single 8 bit byte. Now the task is to come up with a bit pattern which when added to the given bit pattern (00000101) would result in all zeros at the memory location which is used to hold this 5 i.e. all 8 bits of the byte should be zero. To do that, start from the right most bit of 101 and for each individual bit, again ask the same question: What bit should I add to the current bit to make the result zero ? continue doing that taking in account the usual carry over. After we are done with the 3 right most places (the digits that define the original number without regard to the leading zeros) the last carry goes in the bit pattern of the additive inverse. Furthermore, since we are holding in the original number in a single 8 bit byte, all other leading bits in the additive inverse should also be 1's so that (and this is important) when the computer adds "the number" (represented using the 8 bit pattern) and its additive inverse using "that" storage type (a byte) the result in that byte would be all zeros.

     1 1 1
     ----------
       1 0 1
     1 0 1 1 ---> additive inverse
      ---------
       0 0 0
    
    0 讨论(0)
提交回复
热议问题