Why we need to add 1 while doing 2's complement

℡╲_俬逩灬. 提交于 2019-11-30 10:14:02

Your error is in "we do one's compliment and get -7". To see why this is wrong, take the one's complement of 7 and add 7 to it. If it's -7, you should get zero because -7 + 7 = 0. You won't.

The one's complement of 7 was 1000. Add 7 to that, and you get 1111. Definitely not zero. You need to add one more to it to get zero!

The negative of a number is the number you need to add to it to get zero.

If you add 1 to ...11111, you get zero. Thus -1 is represented as all 1 bits.

If you add a number, say x, to its 1's complement ~x, you get all 1 bits.

Thus:
~x + x = -1

Add 1 to both sides:
~x + x + 1 = 0

Subtract x from both sides:
~x + 1 = -x

The +1 is added so that the carry over in the technique is taken care of.

Take the 7 and -7 example.

If you represent 7 as 00000111

In order to find -7:

Invert all bits and add one

11111000 -> 11111001

Now you can add following standard math rules:

  00000111
+ 11111001
 -----------
  00000000

For the computer this operation is relatively easy, as it involves basically comparing bit by bit and carrying one.

If instead you represented -7 as 10000111, this won't make sense:

   00000111
+  10000111
  -----------
   10001110 (-14)

To add them, you will involve more complex rules like analyzing the first bit, and transforming the values.

A more detailed explanation can be found here.

Short answer: If you don't add 1 then you have two different representations of the number 0.

Longer answer: In one's complement

  • the values from 0000 to 0111 represent the numbers from 0 to 7

  • the values from 1111 to 1000 represent the numbers from 0 to -7

    • since their inverses are 0000 and 0111.

There is the problem, now you have 2 different ways of writing the same number, both 0000 and 1111 represent 0.

If you add 1 to these inverses they become 0001 and 1000 and represent the numbers from -1 to -8 therefore you avoid duplicates.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!