The World’s Largest Online Community for Developers
Does the C++11 standard guarantee that the unary minus of a zero-valued signed integer is zero?
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 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.