20

C17 6.2.6.2/4 says:

If the implementation does not support negative zeros, the behavior of the &, |, ^, ~, <<, and >> operators with operands that would produce such a value is undefined.

If I have a 2's complement system, it does not support negative zeroes. And it always utilizes all possible combinations of a binary number to express a value. Therefore it is impossible to ever produce a negative zero, no matter which bitwise operation that is used. So what is the meaning of this text?

My take is that this part refers to systems with 1's complement or signed magnitude that does not support negative zeroes, but instead use a padding bit or trap representation. Is this correct?

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • What do you understand by "trap representation" ? – alinsoar Mar 07 '19 at 14:25
  • I also read lots of times about "tr" but I never saw one concretely, so I cannot understand what that means. I read articles about this, but I do not understand what is a trap representation. On X86 processors are there trs ? – alinsoar Mar 07 '19 at 14:57
  • 1
    @alinsoar I don't know of any implementation in the real world that uses them for plain integers (floating point is another story). Mostly it is something invented by C standard committee language lawyers, like they invented the need to support 1's complement and signed magnitude computers. – Lundin Mar 07 '19 at 15:24
  • 2
    I don't get why there are complicated answers and why this is even a question, doesn't it just mean exactly what it says? You are asking about a system which does not contain the notion of a negative zero, so isn't that _exactly what that section would be describing_? Meaning, just as your quoted section says... the behavior that would produce a negative zero (that behavior which doesn't exist in your case) has undefined results. As simple as that. Analogy: Enacting a law saying "Anyone who slays a boogeyman gets a week in jail" is not suddenly confusing simply because of a lack of boogeymen. – Loduwijk Mar 07 '19 at 19:44
  • 1
    @Aaron Not exactly. At the bottom of my answer is an example that shows this can happen, and when it does [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) is invoked. – dbush Mar 07 '19 at 20:31
  • @dbush But still, as I suggested, it is then undefined behavior... exactly as OP's quote suggests. I don't get how the rule "When -0 is not supported, it is undefined" can result in the question "When -0 is not supported, what does this mean?" Perhaps the answer is to explain to OP what undefined means? I'm not sure. I'm confused by this question itself. – Loduwijk Mar 07 '19 at 21:03
  • @Aaron The confusion here was because C allows three different representations: 2's complement, 1's complement and signed magnitude. It is reasonable to assume that the latter two support negative zero and the former does not. Paragraph 3 just before the quoted one says: "If the implementation supports negative zeros,...". So I incorrectly read this as §3 is for 1's complement and signed magnitude, §4 is for 2's complement, which made me confused. The follow-up question to the committee would then be: which 1's complement/signed magnitude systems with trap representations does this refer to... – Lundin Mar 08 '19 at 08:52

2 Answers2

15

Yes, I think your interpretation is correct. In two's complement, that are no operations that could generate a negative zero, because the concept here doesn't exist: any value that has the sign bit set is necessarily less than 0.

BTW: It is very likely that the exotic sign representations will be removed from C2x, so all of this will disappear.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 1
    I'm amazed at why language designers seem to be so worried about backwards _hardware_ compatibility. I don't think any computer hardware commercialised during the current century uses anything but twos complement representation, and one can be rather certain that nobody is going to write a compiler for a new language version designed to work on old hardware (the opposite, running an old language version on new hardware, of course happens a lot). So language designers could have assumed twos complement behaviour for a long time, but apparently they are only coming around to doing that just now. – Marc van Leeuwen Mar 07 '19 at 15:16
  • @Jens, is the committee then considering specifying two's complement representation as the only option? For another way the exotic sign representations might be removed would be to delete the *details*, but simply make the representations of signed integers implementation defined (or even unspecified) when the sign bit is set. – John Bollinger Mar 07 '19 at 15:47
  • 1
    @MarcvanLeeuwen, the problem was that it is difficult to assess if there are still such implementations around. And, as always, if people would more engage in committee work, things might go smoother. – Jens Gustedt Mar 07 '19 at 17:19
  • @JohnBollinger yes, it should be two's complement, see http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2330.pdf – Jens Gustedt Mar 07 '19 at 17:23
  • 2
    @JohnBollinger: If I had my druthers, the Standard would recognize a category of implementation where all integer types' representations were either consistently big-endian or little-endian without padding and signed integers were two's-complement, and a broader category where integer types could be stored in any fashion the implementation sees fit. I see no reason to explicitly allow or exclude any particular forms of representation from the broader category. – supercat Mar 07 '19 at 22:04
12

Your interpretation is correct.

Going up to paragraph 2 of 6.2.6.2:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N ). If the sign bit is zero, it shall not affect the resulting value. 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 ).

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.

This means an implementation using either one's complement or sign and magnitude has, for a given size integer type, a specific representation which must be either negative zero or a trap representation. It's then up to the implementation to choose which one of those applies.

As an example, suppose a system has sign and magnitude representation and a 32 bit int with no padding. Then the representation that would be negative zero, if it is supported, is 0x80000000.

Now suppose the following operations are performed:

 int x = 0x7fffffff;
 x = ~x;

If the implementation supports negative zero, the ~ operator will generate -0 as the result and store it in x. If it does not, it creates a trap representation and invokes undefined behavior as per paragraph 4.

dbush
  • 205,898
  • 23
  • 218
  • 273