二进制补码证明

发布于 2024-09-28 03:51:20 字数 161 浏览 3 评论 0原文

是否可以通过归纳证明,对于所有长度为 n 的序列,任何 0 字符串的二进制补码始终会导致 0?

我尝试使用值公式来执行此操作,即

value = -a_n-1 x 2^(n-1) + summation{i=0 to n} (a_i x 2^i),其中 n = 位数在字符串中

Is it possible to prove by induction that the two's complement of any string of 0's will always result in 0, for all sequences of length n?

I'm trying to do this using the value formula, i.e.

value = -a_n-1 x 2^(n-1) + summation{i=0 to n} (a_i x 2^i), where n = number of bits in string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-10-05 03:51:20

111..111的2的补码不就是1(这意味着111..111代表-1)吗?

Isn't the 2's complement of 111..111 just 1 (which means that 111..111 represents -1)?

例如,您是否要求证明 1111 1111 的二进制补码是 0000 0000?如果是这样,你无法证明它,因为它是假的。 1111 1111 的二进制补码是 0000 0001

    1111 1111
->  0000 0000 <- one's complement
->  0000 0001 <- add 1

对您的编辑的回应:当然。但你不需要感应。将 0_n 的所有位反转以获得补码,得到 1_n 并添加 1 将所有位翻转回零(1 + 1 = 10 并且进位位渗透到我们丢弃它的末尾)。量子电子器件。

Are you asking to prove that, for example, the two's complement of 1111 1111 is 0000 0000? If so, you can not prove it because it is false. The two's complement of 1111 1111 is 0000 0001.

    1111 1111
->  0000 0000 <- one's complement
->  0000 0001 <- add 1

Response to your edit: Sure. But you don't need induction. Inverting all of the bits of 0_n to obtain the one's complement gives you 1_n and adding 1 flips all the bits back to zero (1 + 1 = 10 and the carry bit percolates through to end where we drop it). QED.

没有伤那来痛 2024-10-05 03:51:20

1) X 的二补定义:翻转 X 和 sum 1 的位

2) 两个变量的二进制和为 1 位 (http://www.play-hookey.com/digital/adder.html) (即 b1第一个变量和 b2 第二个变量 b1:X 表示变量中的位 X)

r1 = b1:1 XOR b2:1
carry = b1:1 AND b2:1

2.1) 如果 b1:1 和 b2:1 两个位均为

r1 = 0  (always)
carry = 1 (always)

3) 具有 2 位的两个变量的二进制和

r1 = b1:1 XOR b2:1 
carry1 = b1:1 AND b2:1

r2 = (b1:2 XOR b2:2) XOR carry:1
carry2 = (b1:2 AND b2:2) OR (b1:2 AND carry:1) OR (b2:2 AND carry:1)

3.1) 从 2.1 我们可以减少

carry2 = (b1:2 AND b2:2) OR (b1:2 AND 1) OR (b2:2 AND 1)
carry2 = b1:2 OR b2:2

4 ) 是一个数字零全零。翻转所有位将生成全 1 数字: Ones

5) Bit 0 XOR Anything = Anything(XOR 真值表)

6) 对数字 0 应用 (1)

6.1) 翻转

 Flipping Zero = Ones

6.2) sum 1

 result = Ones + N_One (N_One = 00...001)
 result:1 = 0 (from 2.1)
 carry:1 = 1 (from 2.1)

6.3) 由于 N_One 中除 N_One 之外的所有位:1 为零。

 result:n = (Ones:n XOR N_One:n) XOR carry:(n-1) (from 3)
 result:n = (Ones:n XOR 0) XOR carry:(n-1) (definition of N_One 6.2)
 result:n = Ones:n XOR carry:(n-1)

6.4) 来自 3.1

carry:n = Ones:n OR N_One:n -> if carry:n-1 is 1
carry:n = 1 OR 0            -> if carry:n-1 is 1
carry:n = 1                 -> if carry:n-1 is 1

由于第一个进位 (carry:1) 被定义为 1,所以来自 6.1 所有进位都被定义为 1

7) 来自 6.3 和 6.4

 result:n = Ones:n XOR carry:(n-1)
 result:n = 1 XOR 1
 result:n = 0

对于任何 n 值,证明 (~n+1) 始终为 0。(具有固定位域大小的机器的最后进位总是被忽略)

QED

1) Definition of two complement of X: flip the bits of X and sum 1

2) Binary sum of two variables with 1 bit (http://www.play-hookey.com/digital/adder.html) (being b1 the first variable and b2 the second variable. b1:X denote bit X in the variable)

r1 = b1:1 XOR b2:1
carry = b1:1 AND b2:1

2.1) if both bits are one b1:1 and b2:1

r1 = 0  (always)
carry = 1 (always)

3) Binary sum of two variables with 2 bit

r1 = b1:1 XOR b2:1 
carry1 = b1:1 AND b2:1

r2 = (b1:2 XOR b2:2) XOR carry:1
carry2 = (b1:2 AND b2:2) OR (b1:2 AND carry:1) OR (b2:2 AND carry:1)

3.1) From 2.1 we can reduce

carry2 = (b1:2 AND b2:2) OR (b1:2 AND 1) OR (b2:2 AND 1)
carry2 = b1:2 OR b2:2

4) Be a number Zero all zeros. Flipping all bits will generate an all ones number: Ones

5) Bit 0 XOR Anything = Anything (truth table of XOR)

6) Applying (1) on number Zero

6.1) flip

 Flipping Zero = Ones

6.2) sum 1

 result = Ones + N_One (N_One = 00...001)
 result:1 = 0 (from 2.1)
 carry:1 = 1 (from 2.1)

6.3) As all bits from N_One except N_One:1 are zero.

 result:n = (Ones:n XOR N_One:n) XOR carry:(n-1) (from 3)
 result:n = (Ones:n XOR 0) XOR carry:(n-1) (definition of N_One 6.2)
 result:n = Ones:n XOR carry:(n-1)

6.4) from 3.1

carry:n = Ones:n OR N_One:n -> if carry:n-1 is 1
carry:n = 1 OR 0            -> if carry:n-1 is 1
carry:n = 1                 -> if carry:n-1 is 1

As the first carry (carry:1) is defined as 1 from 6.1 all carries are defined as 1

7) from 6.3 and 6.4

 result:n = Ones:n XOR carry:(n-1)
 result:n = 1 XOR 1
 result:n = 0

For any value of n, proving that (~n+1) is always 0. (the last carry for a machine with fixed bitfield size is always ignored)

QED

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文