The World’s Largest Online Community for Developers

';
c++ - Does the C++11 standard guarantee that the unary minus of a zero-valued signed integer is zero? - LavOzs.Com
c++ c++11 integer arithmetic-expressions unary-operator

Does the C++11 standard guarantee that the unary minus of a zero-valued **signed integer** is zero?

For example:

```
int zero = 0;
int n = -zero;
int m = -0;
assert(memcmp(&n, &zero, sizeof(int)) == 0);
assert(memcmp(&m, &zero, sizeof(int)) == 0);
```

I know that `-0`

and `0`

are identical in two's compliment representation, but I'd like to know if the standard allows for the negation of signed integer zero to be negative-zero for other representations, such as one's compliment or signed-magnitude.

All I could find in the C++11 draft is §5.3.1, paragraph 8:

The operand of the unary-operator shall have arithmetic or unscoped enumeration type and

the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.

I can't find a definition of *negation* within the draft.

**Motivation**: I'm writing a specialized integer number parser for a library (that may be open-sourced eventually) and I want to know if I should be concerned about the possibility of `"-0"`

being interpreted as a negative-zero *signed integer* on uncommon architectures.

**Note**: I already know about negative-zero floating-point numbers.

The standard does not mandate bit patterns for integers, because it is meant to be applicable to the widest possible range of machines. It is entirely possible for a C++ compiler to use ones-complement, where zero and negative zero would be different.

Related

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

unary minus for 0x80000000 (signed and unsigned)

Is signed integer overflow still undefined behavior in C++?

Is left-shifting (<<) a negative integer undefined behavior in C++11?

Understanding the order of conversions, arithmetic conversions, and integer promotions for non-overloaded bitwise operators

Does the C++ standard requires signed integers to have exactly one sign bit?

What is the reason for the existent difference between C and C++ relative to the unary arithmetic operator +

Can bitwise operators have undefined behavior?

How to check whether an int variable contains a legal (not trap representation) value?

C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?

unary minus for 0x80000000 (signed and unsigned)

Is signed integer overflow still undefined behavior in C++?

Is left-shifting (<<) a negative integer undefined behavior in C++11?

Understanding the order of conversions, arithmetic conversions, and integer promotions for non-overloaded bitwise operators

Does the C++ standard requires signed integers to have exactly one sign bit?

What is the reason for the existent difference between C and C++ relative to the unary arithmetic operator +

Can bitwise operators have undefined behavior?

How to check whether an int variable contains a legal (not trap representation) value?