Adding and subtracting two's complement

前端 未结 2 932
一个人的身影
一个人的身影 2020-12-16 05:58

Using six-bit one\'s and two\'s complement representation I am trying to solve the following problem:

12 - 7 

Now, i take 12 in binary and

相关标签:
2条回答
  • 2020-12-16 06:19

    No. The algorithm for two's complement doesn't change based on where the negative value is.

    0 讨论(0)
  • 2020-12-16 06:21

    Using two's complement to represent negative values has the benefit that subtraction and addition are the same. In your case, you can think of 12 - 7 as 12 + (-7). Hence you only need to find the two's complement representation of -7 and add it to +12:

    12  001100
    -7  111001   -- to get this, invert all bits of 7 (000111) and add 1
    ----------
     5 1000101
    

    Then discard the carry (indicates overflow), and you have your result: 000101 which equals to 5 as expected.

    For your example of -15 + 2, simply follow the same procedure to get the two's complement representation of -15:

    15  001111
        110000   -- inverted bits
        110001   -- add 1
    

    Now do the addition as usual:

    -15  110001
      2  000010
    -----------
    res  110011
    

    To see that res indeed equals -13, you can see that it is negative (MSB set). For the magnitude, convert to positive (invert bits, add 1):

    res  110011
         001100  -- inverted bits
         001101  -- add 1
    

    Hence the magnitude is 13 as expected.

    0 讨论(0)
提交回复
热议问题