I know that the integer values 0
and -0
are essentially the same.
But, I am wondering if it is possible to differentiate between them.
For example, how do I know if a variable was assigned -0
?
bool IsNegative(int num)
{
// How ?
}
int num = -0;
int additinon = 5;
num += (IsNegative(num)) ? -addition : addition;
Is the value -0
saved in the memory the exact same way as 0
?
For an
int
(in the almost-universal "2's complement" representation) the representations of0
and-0
are the same. (They can be different for other number representations, eg. IEEE 754 floating point.)If your machine has distinct representations for
-0
and+0
, thenmemcmp
will be able to distinguish them.If padding bits are present, there might actually be multiple representations for values other than zero as well.
It depends on the machine you're targeting.
On a machine that uses a 2's complement representation for integers there's no difference at bit-level between
0
and-0
(they have the same representation)If your machine used one's complement, you definitely could
Obviously we're talking about using native support, x86 series processors have native support for the two's complement representation of signed numbers. Using other representations is definitely possible but would probably be less efficient and require more instructions.
(As JerryCoffin also noted: even if one's complement has been considered mostly for historical reasons, signed magnitude representations are still fairly common and do have a separate representation for negative and positive zero)
Let's begin with representing 0 in 2's complement (of course there exist many other systems and representations, here I'm referring this specific one), assuming 8-bit, zero is:
Now let's flip all the bits and add 1 to get the 2's complement:
we got
0000 0000
, and that's the representation of -0 as well.But note that in 1's complement, signed 0 is 0000 0000, but -0 is 1111 1111.
To simplify i found it easier to visualize.
Type int(_32) is stored with 32 bits. 32 bits means 2^32 = 4294967296 unique values. Thus :
unsigned int data range is 0 to 4,294,967,295
In case of negative values it depends on how they are stored. In case
In case of One's complement value -0 exists.
In the C++ language specification, there is no such int as negative zero.
The only meaning those two words have is the unary operator
-
applied to0
, just as three plus five is just the binary operator+
applied to3
and5
.If there were a distinct negative zero, two's complement (the most common representation of integers types) would be an insufficient representation for C++ implementations, as there is no way to represent two forms of zero.
In contrast, floating points (following IEEE) have separate positive and negative zeroes. They can be distinguished, for example, when dividing 1 by them. Positive zero produces positive infinity; negative zero produces negative infinity.
However, if there happen to be different memory representations of the int 0 (or any int, or any other value of any other type), you can use
memcmp
to discover that:Of course, if this did happen, outside of direct memory operations, the two values would still work in exactly the same way.