是否可以区分0和-0? [英] Is it possible to differentiate between 0 and -0?

查看:227
本文介绍了是否可以区分0和-0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道整数值 0 -0 本质上是一样的。
但是,我想知道是否可以区分它们。

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.

例如,如何知道是否分配了一个变量 -0

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;

-0 内存与 0

推荐答案

在使用 2的对于整数,在 0 -0 位位置没有差异 $ c>(它们具有相同的表示)

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

显然,我们在谈论使用本机支持,x86系列处理器原生支持二进制有符号数的补码表示。使用其他表示是绝对可能的,但是可能效率较低,并且需要更多的指令。

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.

(因为JerryCoffin也注意到:历史原因,签名的幅度表示仍然相当常见,并且有单独的表示为负和正零) sub>

(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)

这篇关于是否可以区分0和-0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆