一个人的补足是一个现实世界的问题,还是一个历史问题?

发布于 2024-07-06 17:39:12 字数 476 浏览 16 评论 0原文

另一个问题询问如何确定 C 中的奇数/偶数,并且惯用的 (x & 1) 方法被正确标记为损坏 < a href="http://en.wikipedia.org/wiki/Signed_number_representations#Ones.27_complement" rel="noreferrer">基于补码的系统,这是 C 标准允许的。

系统真的存在于计算机博物馆之外的“现实世界”中吗? 我从 20 世纪 70 年代就开始编码,而且我很确定我从未遇到过这样的野兽。

是否有人真正为这样的系统开发或测试代码? 如果没有,我们是否应该担心这些事情,或者我们应该将它们与Room 101一起放入纸带和打孔卡...?

Another question asked about determining odd/evenness in C, and the idiomatic (x & 1) approach was correctly flagged as broken for one's complement-based systems, which the C standard allows for.

Do systems really exist in the 'real world' outside of computer museums? I've been coding since the 1970's and I'm pretty sure I've never met such a beast.

Is anyone actually developing or testing code for such a system? And, if not, should we worry about such things or should we put them into Room 101 along with paper tape and punch cards...?

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

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

发布评论

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

评论(9

画离情绘悲伤 2024-07-13 17:39:12

我在遥测领域工作,我们的一些客户拥有仍然使用 1 补码的旧模数转换器。 前几天我只需要编写代码将 1 的补码转换为 2 的补码以进行补偿。

所以是的,它仍然存在(但你不会经常遇到它)。

I work in the telemetry field and we have some of our customers have old analog-to-digital converters that still use 1's complement. I just had to write code the other day to convert from 1's complement to 2's complement in order to compensate.

So yes, it's still out there (but you're not going to run into it very often).

太阳哥哥 2024-07-13 17:39:12

这一切都取决于了解你的根源。
是的,这在技术上是一种古老的技术,我可能会按照其他人在该问题中建议的方式进行操作,并使用模 (%) 运算符来确定奇数或偶数。
但了解什么是 1 补码(或 2 补码)总是一件好事。 无论您是否使用过它们,您的 CPU 一直在处理这些事情。 所以理解这个概念永远不会有什么坏处。 现在,现代系统使您通常不必担心类似的事情,因此它在某种程度上已成为编程 101 课程的主题。 但你必须记住,有些人实际上仍然会在“现实世界”中使用它......例如,与流行的看法相反,有些人仍然使用汇编!不多,但直到 CPU 出现可以理解原始的 C# 和 Java,有人仍然需要理解这些东西。

哎呀,你永远不知道什么时候你会发现自己在做一些实际上需要执行二进制数学的事情,而 1 的补码可能会派上用场。

This all comes down to knowing your roots.
Yes, this is technically an old technique and I would probably do what other people suggested in that question and use the modulo (%) operator to determine odd or even.
But understanding what a 1s complement (or 2s complement) is always a good thing to know. Whether or not you ever use them, your CPU is dealing with those things all of the time. So it can never hurt to understand the concept. Now, modern systems make it so you generally never have to worry about things like that so it has become a topic for Programming 101 courses in a way. But you have to remember that some people actually would still use this in the "real world"... for example, contrary to popular belief there are people who still use assembly! Not many, but until CPUs can understand raw C# and Java, someone is going to still have to understand this stuff.

And heck, you never know when you might find your self doing something where you actually need to perform binary math and that 1s complement could come in handy.

夜夜流光相皎洁 2024-07-13 17:39:12

我在 80 年代使用的 CDC Cyber​​ 18 是一台 1s 补码机器,但那是近 30 年前的事了,从那以后我就再也没有见过它(不过,那也是我最后一次在非 PC 上工作)

The CDC Cyber 18 I used back in the '80 was a 1s complement machine, but that's nearly 30 years ago, and I haven't seen one since (however, that was also the last time I worked on a non-PC)

忆依然 2024-07-13 17:39:12

RFC 791 p.14 将 IP 标头校验和定义为:

校验和字段是标头中所有 16 位字的补码和的 16 位补码。 为了计算校验和,校验和字段的值为零。

因此,在现实世界中,发送的每个 IP 数据包中仍然大量使用补码。 :)

RFC 791 p.14 defines the IP header checksum as:

The checksum field is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header. For purposes of computing the checksum, the value of the checksum field is zero.

So one's complement is still heavily used in the real world, in every single IP packet that is sent. :)

空袭的梦i 2024-07-13 17:39:12

我决定找一个。 Unisys ClearPath 系统有一个 ANSI C 编译器(是的,他们称之为“美国国家标准 C”,甚至 PDF 文档的最后更新也是在 2013 年。该文档可在 在线;

那里签名类型都使用补码表示,具有以下属性:

Type                 | Bits | Range
---------------------+------+-----------------
signed char          |   9  |  -2⁸+1 ...  2⁸-1
signed short         |  18  | -2¹⁷+1 ... 2¹⁷-1
signed int           |  36  | -2³⁵+1 ... 2³⁵-1
signed long int      |  36  | -2³⁵+1 ... 2³⁵-1
signed long long int |  72  | -2⁷¹+1 ... 2⁷¹-1

值得注意的是,它默认也支持非一致的 unsigned intunsigned long,范围从 0 ... 2³⁶ - 2,但可以使用编译指示更改为 0 ... 2³⁶ - 1

I decided to find one. The Unisys ClearPath systems have an ANSI C compiler (yes they call it "American National Standard C" for which even the PDF documentation was last updated in 2013. The documentation is available online;

There the signed types are all using one's complement representation, with the following properties:

Type                 | Bits | Range
---------------------+------+-----------------
signed char          |   9  |  -2⁸+1 ...  2⁸-1
signed short         |  18  | -2¹⁷+1 ... 2¹⁷-1
signed int           |  36  | -2³⁵+1 ... 2³⁵-1
signed long int      |  36  | -2³⁵+1 ... 2³⁵-1
signed long long int |  72  | -2⁷¹+1 ... 2⁷¹-1

Remarkably, it also by default supports non-conforming unsigned int and unsigned long, which range from 0 ... 2³⁶ - 2, but can be changed to 0 ... 2³⁶ - 1 with a pragma.

青衫负雪 2024-07-13 17:39:12

我从来没有遇到过补码系统,而且我和你一样一直在编码。

但我确实遇到了 9 的补码系统——HP-41c 计算器的机器语言。 我承认这可以被认为是过时的,而且我认为他们从来没有针对这些的 C 编译器。

I've never encountered a one's complement system, and I've been coding as long as you have.

But I did encounter a 9's complement system -- the machine language of a HP-41c calculator. I'll admit that this can be considered obsolete, and I don't think they ever had a C compiler for those.

温柔戏命师 2024-07-13 17:39:12

去年的某个时候,我们卸下了最后一台 1960 年代的 Honeyboxen,这使它成为我们现场最古老的机器。 这是二进制补码。 这并不是说了解或意识到自己的互补是一件坏事。 只是,无论他们在工作中让您进行多少计算机考古工作,您今天可能永远不会遇到补码问题。

您在整数方面更有可能遇到的问题是 endian 问题(我正在寻找在您PDP)。 此外,您还会遇到更多“现实世界”(即今天)的问题 浮动 格式 与整数格式不同。

We got off our last 1960's Honeyboxen sometime last year, which made it our oldest machine on site. It was two's complement. This isn't to say knowing or being aware of one's complement is a bad thing. Just, You will probably never run into one's complement issues today, no matter how much computer archeology they have you do at work.

The issues you are more likely to run into on the integer side are endian issues (I'm looking at you PDP). Also, you'll run into more "real world" (i.e. today) issues with floating point formats than you will integer formats.

榆西 2024-07-13 17:39:12

有趣的是,人们在 comp.std.c 1993 年没有人能指出当时使用过的补码机

所以,是的,我认为我们可以自信地说,一个人的补语属于我们历史的一个黑暗角落,几乎已经死了,不再是一个问题。

Funny thing, people asked that same question on comp.std.c in 1993, and nobody could point to a one's complement machine that had been used back then.

So yes, I think we can confidently say that one's complement belongs to a dark corner of our history, practically dead, and is not a concern anymore.

陌路黄昏 2024-07-13 17:39:12

一个人的补码是一个现实世界的问题,还是一个历史问题?

是的,还在用。 它甚至用于现代英特尔处理器。 来自英特尔® 64 和 IA- 32 Architectures 软件开发人员手册 2A,第 3-8 页:

3.1.1.8 描述部分

每条指令都由许多信息部分来描述。 “描述”部分更详细地描述了指令的用途和所需的操作数。

描述部分中可能使用的术语摘要:
* 旧版 SSE:指 SSE、SSE2、SSE3、SSSE3、SSE4、AESNI、PCLMULQDQ 以及引用 XMM 寄存器且不带 VEX 前缀进行编码的任何未来指令集。
* VEX.vvvv。 指定源或目标寄存器的 VEX 位字段(以 1 的补码形式)。
* rm_field:ModR/M r/m 字段和任何 REX.B 的简写
* reg_field:ModR/M reg 字段和任何 REX.R 的简写

Is one's complement a real-world issue, or just a historical one?

Yes, it still used. Its even used in modern Intel processors. From Intel® 64 and IA-32 Architectures Software Developer’s Manual 2A, page 3-8:

3.1.1.8 Description Section

Each instruction is then described by number of information sections. The “Description” section describes the purpose of the instructions and required operands in more detail.

Summary of terms that may be used in the description section:
* Legacy SSE: Refers to SSE, SSE2, SSE3, SSSE3, SSE4, AESNI, PCLMULQDQ and any future instruction sets referencing XMM registers and encoded without a VEX prefix.
* VEX.vvvv. The VEX bitfield specifying a source or destination register (in 1’s complement form).
* rm_field: shorthand for the ModR/M r/m field and any REX.B
* reg_field: shorthand for the ModR/M reg field and any REX.R

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