二进制补码快速方法
Two's Complement Quick Way
求二进制数补码的方法是:
- Let x ̄ = the logical complement of x. The logical complement (also called the one’s complement) is formed by flipping all the bits in the
number, changing all of the 1 bits to 0, and vice versa.
- Let X = x ̄ + 1. If this addition overflows, then the overflow bit is discarded.
By the definition of two’s complement, X ≡ −x.
我看到一个快速的方法,就是:
eg.
B = 00010110
D = 22
Flip everything after the first "1" counting from left side.
-B = 11101010
-D = -22
我无法理解这种方式的证明。
如果你采用定义,-x = ~x + 1
,那么如果我们将 x
表示为字符串 a10k(后跟字符串 a
由 1 后接 k 个零),然后:
-(a10^k) =
// by definition
~(a10^k) + 1 =
// complement distributes over concatenation
~a01^k + 1 =
// carry through the 1s and set the 0
~a10^k
最后的结果,~a10^k
,意思是"complement the left side, until (and not including) the rightmost 1"。
这个证明对 x = 0
不成立,因为它不能写成 a10k 的形式,等价性仍然成立:没有要补充的部分,因为没有最右边的 1,所以结果又是零,这是正确的。
维基百科上的补码
A shortcut to manually convert a binary number into its two's complement is to start at the least significant bit (LSB), and copy all the zeros, working from LSB toward the most significant bit (MSB) until the first 1 is reached; then copy that 1, and flip all the remaining bits (Leave the MSB as a 1 if the initial number was in sign-and-magnitude representation). This shortcut allows a person to convert a number to its two's complement without first forming its ones' complement. For example: in two's complement representation, the negation of "0011 1100" is "1100 0100", where the underlined [bolded] digits were unchanged by the copying operation (while the rest of the digits were flipped).
所以我猜你说的是“从左侧数第一个“1”后翻转所有内容。”需要固定为“翻转从右侧数起的第一个“1”之后的所有内容。”
这是“慢速方式”:
22 十进制 = 00010110 二进制 -> 翻转:11101001 -> 加 1:11101001 + 1 = 11101010 = -22 十进制
求二进制数补码的方法是:
- Let x ̄ = the logical complement of x. The logical complement (also called the one’s complement) is formed by flipping all the bits in the number, changing all of the 1 bits to 0, and vice versa.
- Let X = x ̄ + 1. If this addition overflows, then the overflow bit is discarded. By the definition of two’s complement, X ≡ −x.
我看到一个快速的方法,就是:
eg.
B = 00010110 D = 22
Flip everything after the first "1" counting from left side.
-B = 11101010 -D = -22
我无法理解这种方式的证明。
如果你采用定义,-x = ~x + 1
,那么如果我们将 x
表示为字符串 a10k(后跟字符串 a
由 1 后接 k 个零),然后:
-(a10^k) =
// by definition
~(a10^k) + 1 =
// complement distributes over concatenation
~a01^k + 1 =
// carry through the 1s and set the 0
~a10^k
最后的结果,~a10^k
,意思是"complement the left side, until (and not including) the rightmost 1"。
这个证明对 x = 0
不成立,因为它不能写成 a10k 的形式,等价性仍然成立:没有要补充的部分,因为没有最右边的 1,所以结果又是零,这是正确的。
维基百科上的补码
A shortcut to manually convert a binary number into its two's complement is to start at the least significant bit (LSB), and copy all the zeros, working from LSB toward the most significant bit (MSB) until the first 1 is reached; then copy that 1, and flip all the remaining bits (Leave the MSB as a 1 if the initial number was in sign-and-magnitude representation). This shortcut allows a person to convert a number to its two's complement without first forming its ones' complement. For example: in two's complement representation, the negation of "0011 1100" is "1100 0100", where the underlined [bolded] digits were unchanged by the copying operation (while the rest of the digits were flipped).
所以我猜你说的是“从左侧数第一个“1”后翻转所有内容。”需要固定为“翻转从右侧数起的第一个“1”之后的所有内容。”
这是“慢速方式”: 22 十进制 = 00010110 二进制 -> 翻转:11101001 -> 加 1:11101001 + 1 = 11101010 = -22 十进制