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

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

问题描述

在 2 的补码中,0-127 表示为 00000000 到 01111111.如果是负数,我们将无符号表示中的所有位反转并加 1 以获得 2 的补码.

(参考:http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement)

所以 2 的补码中的 -1 将是:

<上一页>无符号 1 = 00000001反转所有位 = 11111110加 1 = 11111111

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

<上一页>无符号 128 = 10000000反转所有位= 01111111加 1 = 10000000

所以 -128 和 128 在 2 的补码表示法中具有相同的表示?为什么 8 位的 2 补码范围不是 -127 到 128?简而言之,为什么 -128 比使用相同位数表示无符号 128 更受欢迎?

解决方案

没有128"在有符号字节中.范围是

  • 0 到 127:128 个值
  • -1 到 -128:128 个值

总共 256 个值,即 2^8.

附录基于评论(并重新阅读问题)

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

<块引用>

范围内最小数字的二进制补码不会产生取反数字的预期效果.

例如,在 8 位系统中 -128 的二进制补码会产生相同的二进制数.这是因为正值 128 不能用 8 位带符号二进制数表示.请注意,这被检测为溢出条件,因为最高有效位进位但没有进位.这可能会导致意外的错误,因为在最小负数的情况下,未经检查的绝对值实现可能会返回负数.C 中的 abs 系列整数函数通常具有这种行为.Java 也是如此.在这种情况下,由开发人员决定是否在调用函数之前检查最小负值.

二进制补码中最大的负数有时被称为奇怪的数".因为它是唯一的例外.尽管该数字是一个例外,但它在常规二进制补码系统中是一个有效数字.所有算术运算都将其用作操作数和(除非发生溢出)结果.

此外,右移一个有符号整数会使 CPU 将 MSb(第 7 位)传播到右侧,如果 0x80 为 +128,这将违反简单的逻辑,因为只有一个移位,我们将获得 0xC0 ,它是一个负数 (-64)...(而从正数右移通常永远不会产生负数结果).

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.

(Reference: http://en.wikipedia.org/wiki/Signed_number_representations#Two.27s_complement)

so -1 in 2's complement will be:


 unsigned 1 =      00000001

 invert all bits = 11111110

 add 1 =           11111111

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


 unsigned 128 =    10000000

 invert all bits=  01111111

 add 1=            10000000

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?

解决方案

There is no "128" in a signed byte. The range is

  • 0 to 127 : 128 values
  • -1 to -128 : 128 values

Total 256 values, ie 2^8.

Addendum based on comment (and rereading the question)

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

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

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.

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).

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

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