1 和 2 补码系统

发布于 2024-10-10 06:07:00 字数 316 浏览 2 评论 0原文

我试图了解这两个系统之间的差异及其对 C 编程的影响。

根据我从维基百科了解到的信息:

  1. 两个系统都用于表示负数
  2. 补码按位 NOT 应用于负数(系统有 +0 和 -0)
  3. 补码按步骤 2 执行并加 1(消除 +/- 0)

我还遗漏了什么吗?

我的问题:

  1. 哪些架构支持哪些系统?现在最常见的是什么(1 或 2 的补码)?
  2. 当用 C 语言编程时,我们应该在什么意义上考虑这些系统?它主要只在嵌入式世界中有意义吗?

提前致谢!

I'm trying to understand the differences between these two systems and their impact on C programming.

From what I've learned from Wikipedia:

  1. both systems are used to represent negative numbers
  2. one's complement applies bitwise NOT to negative number (the system has +0 and -0)
  3. two's complement does as in step 2 and adds 1 (eliminates +/-0)

Am I missing something else?

My questions:

  1. which architectures support which system? What is the most common these days (1's or 2's complement)?
  2. in what sense should we consider these systems when programming in C? Does it mainly make sense only in embedded world?

Thanks in advance!

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

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

发布评论

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

评论(2

Bonjour°[大白 2024-10-17 06:07:00

现在大多数系统都使用二进制补码,因为它允许计算机执行相同的加法/减法操作,而无需关心数字的特定符号。

当你编程时,无论使用什么系统,算术都可以工作——数据类型的范围是由语言定义的,所以如果它说一个类型将在 -2^31 到 +2^31 - 1 的范围内工作,那么无论符号如何,它都会起作用。不过,在处理单个位或位移位时,您需要小心 - 这些行为不会像非补码系统中的二次幂算术那样(尽管您不太可能遇到此类系统,并且可能如果您只使用电脑,则永远不会)。

Most systems nowadays use two's complement, since it lets the computer do the same exact operation for addition/subtraction without caring about the particular sign of the number.

When you're programming, the arithmetic works regardless of the system used -- the range of the data types are defined by the language, so if it says a type will work in the range -2^31 to +2^31 - 1, then it'll work regardless of the notation. You need to be careful when working with individual bits or bit shifts, though -- those won't behave like power-of-two arithmetic in non-two's complement systems (although you're not too likely to encounter such systems, and probably never will, if you're just working with PCs).

无敌元气妹 2024-10-17 06:07:00

整数的补码表示法的唯一优点是它允许在没有进位链的情况下执行符号量值形式的转换。如果寄存器使用补码形式,那么用一组闪烁灯来构建以符号数值形式显示每个寄存器的值的计算机将比使用补码形式更方便。如果想要对闪烁灯和 CPU 寄存器使用单独的存储锁存器,最简单的适应方法是使用一个电路将二进制补码转换为二进制补码或符号数值形式,然后让每个寄存器同时写入将二进制补码值存储在寄存器中,同时用符号幅度值更新闪烁灯锁存器。然而,锁存电路非常昂贵,如果寄存器是由离散锁存器构建的,则向 ALU 添加一些电路以使其使用反码,然后从 CPU 的“真实”寄存器馈送信号可能是比为灯添加一套额外的闩锁便宜。

当然,在过去的几十年里,不同电路元件的相对成本已经转变到了这样的地步:用灯来直接报告 CPU 寄存器的状态是荒谬的。因此,补码设计过去可能具有的实际优势不再适用。

The only advantage of ones'-complement notation for integers is that it allows conversions to and from sign-magnitude form to be performed without a carry chain. Building a computer with a set of blinkenlights that show each register's value in sign-magnitude form will be much more convenient if the registers use ones'-complement form than if they use two's-complement form. If one wanted to use separate storage latches for the blinkenlights and the CPU's registers, the easiest way to accommodate things would be to have one circuit which translates two's-complement to one's-complement or sign-magnitude form, and then have each register write simultaneously store the two's-complement value in the register while updating the blinkenlight latches with the sign-magnitude value. Latching circuitry is sufficiently expensive, however, that if registers are being built out of discrete latches anyway, adding some circuitry to the ALU to make it use ones'-complement, and then feeding the lights from the CPU's "real" registers, may be cheaper than including an extra set of latches for the lights.

Over the last few decades, of course, the relative costs of different circuit elements have shifted to the point that it would be absurd to have lights wired to directly report the state of a CPU's registers. Consequently, the practical advantages that ones'-complement designs might have had in the past are no longer applicable.

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