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

后端 未结 9 791
广开言路
广开言路 2020-11-29 04:53

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

相关标签:
9条回答
  • 2020-11-29 04:59

    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.

    0 讨论(0)
  • 2020-11-29 05:01

    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

    0 讨论(0)
  • 2020-11-29 05:04

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

    0 讨论(0)
  • 2020-11-29 05:07

    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.

    0 讨论(0)
  • 2020-11-29 05:18

    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)

    0 讨论(0)
  • 2020-11-29 05:20

    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.

    0 讨论(0)
提交回复
热议问题