Is it possible to differentiate between 0 and -0?

2020-05-16 13:04发布

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?

7条回答
倾城 Initia
2楼-- · 2020-05-16 13:43

For an int (in the almost-universal "2's complement" representation) the representations of 0 and -0 are the same. (They can be different for other number representations, eg. IEEE 754 floating point.)

查看更多
叛逆
3楼-- · 2020-05-16 13:47

If your machine has distinct representations for -0 and +0, then memcmp will be able to distinguish them.

If padding bits are present, there might actually be multiple representations for values other than zero as well.

查看更多
SAY GOODBYE
4楼-- · 2020-05-16 13:48

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

0000 0000   -> signed   0 
1111 1111   -> signed   −0

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)

查看更多
地球回转人心会变
5楼-- · 2020-05-16 13:49

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:

0000 0000

Now let's flip all the bits and add 1 to get the 2's complement:

1111 1111 (flip)
0000 0001 (add one)
---------
0000 0000

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.

查看更多
Root(大扎)
6楼-- · 2020-05-16 13:53

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.

查看更多
男人必须洒脱
7楼-- · 2020-05-16 14:04

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 to 0, just as three plus five is just the binary operator + applied to 3 and 5.

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:

#include <string>

int main() {
    int a = ...
    int b = ...
    if (memcmp(&a, &b, sizeof(int))) {
        // a and b have different representations in memory
    }
}

Of course, if this did happen, outside of direct memory operations, the two values would still work in exactly the same way.

查看更多
登录 后发表回答