3

I generally understand the concept of 2's complement. When converting a value to 2's complement for subtraction, simply negate each digit of the number to get 1's complement and add 1. When calculating the result of something like 2-2 in binary, you first convert -2 to 2's complement, (I like to use 4 bit representations) 0010 is turned into 1101 and then into 1110. Then you evaluate 0010 + 1110 and get 0000 and the overflowed 1 is truncated.

However, what about a problem like 2-3? So I did the same operation. 2 is 0010 and the 2's complement of 3 (0011) is 1101. So 0010 + 1101 results in 1111 and no overflow. The result is 15. 15 is not negative 1. Am I missing a crucial point about how 2's complement works that other websites and sources neglect to mention?

Orren Ravid
  • 560
  • 1
  • 6
  • 24
  • 3
    What is -1 in 2's complement? – Oliver Charlesworth Dec 24 '15 at 18:00
  • So I guess my question wasn't specific enough. So that means that any time I do subtraction, the result will be in 2's complement which must then undergo the 2's complement operation again in order to receive an unsigned binary representation? – Orren Ravid Dec 24 '15 at 18:10

3 Answers3

1

In 2's compliment 1111 is -1 (if you only have 4 bits of course)

From 1111 to its absolute value invert:

0000

and add 1

0001

Andrew
  • 698
  • 5
  • 13
  • So is the result of subtraction always in the form of 2's complement which must then be reverted back to its unsigned binary representation? – Orren Ravid Dec 24 '15 at 18:08
  • If you want to see the absolute value, yes. If you want to continue to use it for arithmetic then leave it as it is. – Andrew Dec 24 '15 at 18:11
  • @OrrenRavid. -1 doesn't have an unsigned binary representation. You're doing arithmetic in 2's complement; you get an answer in 2's complement. That answer is the 2's complement representation of -1. – Oliver Charlesworth Dec 24 '15 at 18:11
  • @OrrenRavid When you calculate in 2s complement, all results will of course be in 2s complement. You have to choose if you prepare for overflows, if you want signed or unsigned etc. – Sami Kuhmonen Dec 24 '15 at 18:12
  • So does that mean then that all 2's complement arithmetic done with 4 bit representations can only be done from 0000 to 0111 for positive values and 1000 to 1111 for negative values? – Orren Ravid Dec 24 '15 at 18:14
0

Remember that you are doing signed arithmetic.

In signed arithmetic, the 1 in the most significant bit position, or leftmost position indicates a (-) sign.

So 1111 is a negative quantity.

1111 flipped is 0000 adding 1 is 0001.

So the answer has magnitude 1 and sign (-).

2-3=-1

Dylan Kirkby
  • 1,427
  • 11
  • 19
0

When you calculate in twos complement, the result will always have to be handled as twos complement. As mentioned, the result is -1 in signed twos complement, or 15 as unsigned without overflow.

What you have to notice is that if you do subtraction with positive numbers and the result will be negative you will not get overflow. But if the result is positive there will be overflow.

This is logical: if the result is positive, it has to be smaller than the original value. The only way for an addition to result in a smaller value is to overflow. When the result is negative it will always be larger than the original when handled as unsigned (MSB will be 1 whereas original will have 0) and there is no overflow.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74