5 ms·
Au contraire, mon ami: this feature is supported on all major compilers. ;)
by Kronopath 11y ago
Au contraire, mon ami: this feature is supported on all major compilers. ;)
- dwulfen 11y agothis is effectively bitwise math, so it's actually supported in any language offering the minus and binary complement prefix operators. Drop this into your address bar: javascript:var%20x=100;alert("x="+x+"\nx-1="+(~-x)+"\nx+1="+(-~x));
- CamperBob2 11y agoI'm pretty sure that C doesn't guarantee a two's-complement representation. Not so sure about C++. Either way, it makes for a cute novelty article, and a really terrible idea. Raymond should've saved it for the next April Fools' Day.
- dllthomas 11y agoIt's supported by all major compilers, but some may have buggy implementations if you try to target a non-two's-complement architecture. Clearly they need to fix those bugs!
- CamperBob2 11y agoRight, the tilde operator is supported, but the math trick in question only works on a twos-complement machine. So it's non-portable in the sense that the language spec doesn't guarantee a particular behavior.
- dllthomas 11y agoYup. If it wasn't clear, my comment above was a joke :-P
- kps 11y agoYou got it. Both C (C11 §6.2.6.2) and C++ (C11 §3.9.1) permit two's complement, one's complement, and signed magnitude representation of integers. With one's complement, “−” ≡ “~” so “−~” and “~−” both do nothing. With signed magnitude, “−~” and “~−” both give the same result, which is the difference between the value and the maximum integer of the same sign.
- tedajax 11y agoIs it unreasonable for me to want a C/++ standard that's just "What's mostly standard by all major compilers" so we can stop nitpicking about implementation details most people never encounter?
- kps 11y agoAt the time the first version of the C standard was being prepared, the practice of writing C that would only work with the ‘major compiler[s]’ had the nickname “All the world's a VAX”. Now consider the last time you used a VAX.
- userbinator 11y agoThe VAX is a little-endian, 8-bit bytes, 32-bit datapath, 2's complement machine. Consider the last time you used a machine with big endian, non-8-bit-bytes, and something other than 2's complement integer representation... At the time of the C standard, architectures were far more diverse; for better or worse, most of the more esoteric ones seem to have died off, leaving us with the ones that do happen to share many characteristics with a VAX.
- douche 11y agoOut of curiosity, are there any common architectures that are wildly different on those basises from x86/x64/ARM?
- userbinator 11y agoI can't think of any general-purpose architectures in common use which are really odd, but some DSPs have a 24-bit "byte". The microcontroller world is a bit more diverse too, especially with many of them being Harvard architectures (e.g. C compiles to 8051 with 24-bit big-endian pointers, and the various PICs have odd instruction-word sizes like 14 bits). However, all of those still use 2's-complement integers. According to Wikipedia a few existing mainframes use 1's complement (CDC 3000/6000, UNIVAC 1100/2200). I know the standard allows different bit representations, but practically speaking, today you're almost never going to find anything other than 2's complement integers and IEEE floating-point; and if you do happen to be working on one of those rare and unusual machines that don't, I think it'd be the least of your worries... even Linus says he doesn't care about Linux running on non-two's-complement or odd byte-size machines.