本文主要是介绍one's-complement 反码, two's-complement 补码, one's complement sum, two's complement sum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.one's-complement: 反码,高位为符号位;
two's-complement: 补码,高位为符号位;
2. one's complement sum 反码加法,需要加上进位;
two's complement sum 补码加法,丢弃进位进位;
例举一个网上的例子:
2's complement fixed point integers (8-bit)
Binary | Decimal | Hex |
0000 0000 | 0 | 00 |
0000 0001 | 1 | 01 |
0000 0010 | 2 | 02 |
0000 0011 | 3 | 03 |
1111 1111 | -1 | FF |
1111 1110 | -2 | FE |
1111 1101 | -3 | FD |
Let's add two intergers:
-3 + 5 = 2
FD + 05 = 01 02
Discarding the carry (01) gives the correct result.
1's complement fixed point integers (8-bit)
Binary | Decimal | Hex |
0000 0000 | 0 | 00 |
0000 0001 | 1 | 01 |
0000 0010 | 2 | 02 |
0000 0011 | 3 | 03 |
1111 1111 | -0 | FF |
1111 1110 | -1 | FE |
1111 1101 | -2 | FD |
1111 1100 | -3 | FC |
Add the same numbers:
-3 + 5 = 2
FC + 05 = 01 01
Adding the carry (01) to the LSB (01) gives the correct result:
01 + 01 = 02
So, the 1's complement sum is done by summing the numbers and adding the carry (or carries) to the result..
参考:Short description of the Internet checksum,http://www.netfor2.com/checksum.html
这篇关于one's-complement 反码, two's-complement 补码, one's complement sum, two's complement sum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!