-128和128 2的补 [英] -128 and 128 in 2's complement

查看:152
本文介绍了-128和128 2的补的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2的补数,0-127重新presented为00000000到01111111.在负数的情况下,我们颠倒所有位无符号重新presentation加1得到2的补。

In 2's complement, 0-127 is represented as 00000000 to 01111111. In case of negative numbers, we invert all bits in the unsigned representation and add 1 to get the 2's complement.

(参考:<一href=\"http://en.wikipedia.org/wiki/Signed_number_re$p$psentations#Two.27s_complement\">http://en.wikipedia.org/wiki/Signed_number_re$p$psentations#Two.27s_complement)

所以-1 2的补将是:

so -1 in 2's complement will be:



 unsigned 1 =      00000001

 invert all bits = 11111110

 add 1 =           11111111

但对于-128,如果我们按照相同的步骤:

But for -128, if we follow the same steps:



 unsigned 128 =    10000000

 invert all bits=  01111111

 add 1=            10000000

所以-128和128在2的补码相同的再次presentation?为什么不补为8位的范围给定为-127到128?总之,为什么-128 preferred以上重新presenting无符号使用128相同数量的比特的?

so -128 and 128 have the same representation in 2's complement notation? Why isn't the range of 2's complement for 8 bits given as -127 to 128? In short, why is -128 preferred over representing unsigned 128 using the same number of bits?

推荐答案

有没有128。范围是


  • 0到127:128值

  • -1至-128:128值

共256个值,即2 ^ 8。

Total 256 values, ie 2^8.

附录的基础上评论(重读与问题)

Addendum based on comment (and rereading the question)

0x80的可能被视为-128或+128。 维基百科的解释是值得一读

0x80 could have been considered as -128, or +128. Wikipedia explanation is worth reading

2的补不会有否定的数目期望的效果。

The two's complement of the minimum number in the range will not have the desired effect of negating the number.

例如,在一个8比特系统的结果在相同的二进制数的二进制-128补充。这是因为,128正值不能被重新使用8位带符号二进制数psented $ P $。请注意,这被检测为溢出的情况,因为有一个进不能出的最显著位。这可能导致在一个未经检查的实施绝对值可以在最负的情况下,返回一个负数意外错误。在c整数功能的ABS家庭通常有这种行为。这也是真实的Java。在这种情况下,它是为开发者决定是否会有对函数的调用之前的最小负值的检查。

For example, the two's complement of −128 in an 8-bit system results in the same binary number. This is because a positive value of 128 cannot be represented with an 8-bit signed binary numeral. Note that this is detected as an overflow condition since there was a carry into but not out of the most-significant bit. This can lead to unexpected bugs in that an unchecked implementation of absolute value could return a negative number in the case of the minimum negative. The abs family of integer functions in C typically has this behaviour. This is also true for Java. In this case it is for the developer to decide if there will be a check for the minimum negative value before the call of the function.

在二的补码的最负数有时被称为的奇异数,因为它是唯一的例外。虽然数量是一个例外,它是在常规的二的补码系统的有效数字。所有的算术运算与它的工作都作为一个操作数(除非有一个溢出)的结果。

The most negative number in two's complement is sometimes called "the weird number," because it is the only exception. Although the number is an exception, it is a valid number in regular two's complement systems. All arithmetic operations work with it both as an operand and (unless there was an overflow) a result.

此外,右移一个有符号整数将有CPU传播MSB(第7位)的权利,这将是对简单的逻辑,如果 0x80的是+128 ,因为,只有一个班次后,我们将获得将0xC0 这是一个负数(-64)...(在从正数右移即可,通常,从不的产生负的结果)。

Furthermore, right-shifting a signed integer would have the CPU propagate the MSb (bit 7) to the right, which would be against simple logic if 0x80 is +128, as, after only one shift, we would obtain 0xC0 which is a negative number (-64)... (while a right-shift from a positive number can, normally, never produce a negative result).

这篇关于-128和128 2的补的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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