Is there something in the C standard that implies only one representation for signed integers should be used? Specifically, does the standard prohibit using, for example, one's complement for int variables and two's complement for long int variables on the same machine?
If this is related to the hardware rather than the compiler, is there any hardware that allows the existence of two different representations for signed integers on the same machine?
Is there something in the C standard that implies only one representation for signed integers should be used? Specifically, does the standard prohibit using, for example, one's complement for int variables and two's complement for long int variables on the same machine?
If this is related to the hardware rather than the compiler, is there any hardware that allows the existence of two different representations for signed integers on the same machine?
Share Improve this question asked Jan 18 at 11:20 Some Old Jaded GuySome Old Jaded Guy 655 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 2Versions of the C standard prior to 2024 did not specify that different signed integer types had to use the same choice of two’s complement, one’s complement, or sign-and-magnitude.
C 2024 specifies that signed integer types use two’s complement.
The current standard ISO 9899:2024 ("C23") only allows two's complement.
But to clarify what was allowed before:
Specifically, does the standard prohibit using, for example, one's complement for int variables and two's complement for long int variables
Yes it does, you cannot mix different signedness in the same implementation. Representation of integer types 6.2.6.2 in ISO C17 or older stated, emphasis mine:
For signed integer types... /--/ If the sign bit is one, the value shall be modified in one of the following ways:
- the corresponding value with sign bit 0 is negated (sign and magnitude);
- the sign bit has the value -(2M) (two’s complement);
- the sign bit has the value -(2M - 1) (ones’ complement).
The above applies to all the default integer types, save for the optional so-called fixed-width integer types (int32_t
etc) from stdint.h, which only ever allowed two's complement.
If this is related to the hardware rather than the compiler
It is mainly related to the hardware.
is there any hardware that allows the existence of two different representations for signed integers on the same machine?
Very unlikely and I've never heard of it. It is difficult enough to find any historical system which only had one's complement or only sign & magnitude.
static_assert()
) yet need not handle beyond that. – chux Commented Jan 18 at 12:43