为什么我们需要添加1,在做2的补 [英] Why we need to add 1 while doing 2's complement

查看:129
本文介绍了为什么我们需要添加1,在做2的补的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于2的数值,即由重N位psented $ P $补为2 ^ N个。结果
例如:如果数字是7(0111),我再$ P $使用4位,然后它psenting,2对它的补充将是(2 ^ N个),即(2 ^ 4-7)= 9(1001 )结果

The 2's complement of a number which is represented by N bits is 2^N-number.
For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001)

7==> 0111
1's compliment of 7==> 1000
1000
+  1
-------------
1001 =====> (9)

在计算2的一数目的补体,我们以下步骤:
1.做数的补
2.添加一个步骤1的结果。

While calculating 2's complement of a number, we do following steps: 1. do one's complement of the number 2. Add one to the result of step 1.

据我了解,我们需要做数的补,因为我们正在做一个否定操作。但是为什么我们添加了1?

I understand that we need to do one's complement of the number because we are doing a negation operation. But why do we add the 1?

这可能是一个愚蠢的问题,但我有一个很难理解的逻辑。用上面的例子解释(为7号),我们做一补,让-7,然后添加+1,所以-7 + 1 = -6,但我们仍然不能在正确的答案,即+9

This might be a silly question but I'm having a hard time understanding the logic. To explain with above example (for number 7), we do one's complement and get -7 and then add +1, so -7+1=-6, but still we are getting the correct answer i.e. +9

推荐答案

您错误是在我们做一个人的称赞,并得到-7。为了说明为什么这是错的,采取7人的补充和增加7到它。如果是-7,你应该为零,因为-7 + 7 = 0,你不会的。

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.

中7的补是 1000 。加入7到,你会得到 1111 。绝对不是零。你需要一个更添加它来获得零!

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.

如果您添加 1 ... 11111 ,你就会得到零。因此,-1,再presented所有1位。

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

如果您添加一个数字,说 X ,其1的补〜X ,你得到所有1位

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

这样:结果
〜X + X = -1

Thus:
~x + x = -1

加1到两边:结果
〜X + X + 1 = 0

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

从两边减去X:结果
〜X + 1 = -x

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

这篇关于为什么我们需要添加1,在做2的补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆