4 ms·
That's how you end up with student believing dangerous falsehoods "signed integers are 2's complement". This is true in x86, ARM, RISC-V, and just about any ar
by loup-vaillant 8y ago
That's how you end up with student believing dangerous falsehoods "signed integers are 2's complement".
This is true in x86, ARM, RISC-V, and just about any architecture I can think of, but it is not quite true in C, even when it is implemented on top of a 2's complement architecture. Signed integer overflow is undefined, you have to explicitly convert to unsigned before you do any overflow. Only then do you get the implementation defined behaviour your sought, instead of the usual nasal demons.
Also, assembly doesn't mean much with out of order cores. Data dependencies are likely more important than the generated assembly.
- enriquto 8y ago> That's how you end up with student believing dangerous falsehoods "signed integers are 2's complement". 100% agree that this is a dangerous falsehood. I would add the word "always" in your sentence to clarify its falsehood. But there is an even more dangerous, and more insiduous falsehood: "integers are encoded in some mysterious way that is useless to know".
- jandrese 8y agoI wonder how much heartache it would be for the next revision of the C standard to go: Integers behave as if they were two's complement for all overflow behavior. It's already true in just about every system in the world today, and people with extremely obscure hardware can just hack a workaround into their compiler (or straight up ignore the standard because they're just a handful of people anyway). I get that back in the day the K&R didn't want to impose on the hardware vendors and left the standard vague where they had different ideas, but that doesn't mean you can't evolve the standard as the hardware evolves over the decades.
- loup-vaillant 8y agoWhy is this getting downvoted? It sounds very reasonable to me. It wouldn't even be the first time C breaks compatibility (I'm thinking of strict aliasing).
- jandrese 8y agoIt doesn't even break compatibility. It just defines and area that was previously left undefined. It would only break things that were relying on undefined behavior.
- loup-vaillant 8y agoIt wouldn't break overflow, but it would break the conversion to and from signed negative integers (which if I recall correctly is implementation defined, and behaves differently on 1's complement and sign & magnitude platforms —if there are any left).