最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Different representation for signed integer on the same platform - Stack Overflow

programmeradmin1浏览0评论

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
  • 3 C23 now requires two's complement, because that is what current hardware uses. – BoP Commented Jan 18 at 11:38
  • 1 You are right, but what about pre C23. I am using C99 compiler. – Some Old Jaded Guy Commented Jan 18 at 11:43
  • 1 C23 was changed to two's complement, because nobody could find a system that used anything else. – BoP Commented Jan 18 at 11:45
  • Ref C23 – chux Commented Jan 18 at 12:38
  • 1 Although I have never come across an implementation that had mixed signed integer encoding, I have, once, came across an implementation with padding in some larger integer type. I just do not see your concern as one needs to do more that detect, (e.g. static_assert()) yet need not handle beyond that. – chux Commented Jan 18 at 12:43
 |  Show 2 more comments

2 Answers 2

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论