签名int -0? [英] Signed int -0 ?

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

问题描述

签名的int保留一位来表示数字是正数还是b
。鉴于此,一位同事问我C ++中是否存在一个-0
int为-0,负位设置为零。我很感兴趣

,所以我尝试了以下代码:


#include< stdio.h>


int main(int,char **){

int a(-0);

printf(" a =%d \ n",a );

if(a == 0)printf(" a == 0 \ n");

if(a == - 0)printf(" ; a == - 0 \ n");

返回0;

}


输出为:


a = 0

a == 0

a == - 0


所以看来没有这样的数字可以在C ++中使用,并且说到数学数字,它是完全合理的,但实际上是怎么回事

behing the curtains在C ++中,因为编译后的代码无法识别

+0和-0?


-

Christian Stigen Larsen - http://sublevel3.org/~csl/

A signed int reserves one bit to signify whether a number is positive or
negative. In light of this, a colleague asked me whether there existed an
int in C++ that was -0, a zero with the negative bit set. I was intrigued
by this, so I tried the following code:

#include <stdio.h>

int main(int, char**) {
int a(-0);
printf("a=%d\n", a);
if ( a==0 ) printf("a==0\n");
if ( a==-0 ) printf("a==-0\n");
return 0;
}

The output is:

a=0
a==0
a==-0

So it seems that no such number can be used in C++, and speaking of
mathematical numbers, it makes perfect sense, but what is actually going on
"behing the curtains" in C++ since the compiled code can''t discern between
+0 and -0 ?

--
Christian Stigen Larsen -- http://sublevel3.org/~csl/

推荐答案

2003年7月3日星期四13:38:13 +0000(UTC), cs ****** @ newscache.ntnu.no

(Christian Stigen Larsen)写道:
On Thu, 3 Jul 2003 13:38:13 +0000 (UTC), cs******@newscache.ntnu.no
(Christian Stigen Larsen) wrote:
签名的int保留一位来表示数字是正数还是负数。鉴于此,一位同事问我在C ++中是否存在一个为-0的int,其中负位设置为零。我对此很感兴趣,所以我尝试了以下代码:

#include< stdio.h>

int main(int,char **) {
int a(-0);
printf(" a =%d \ n",a);
if(a == 0)printf(" a == 0; \\ n");
if(a == - 0)printf(" a == - 0 \ n");
返回0;
}

输出是:

a = 0
a == - 0

所以似乎没有这样的数字可以在C ++中使用,并且说到数学数字,它是完全合理的,但实际上正在发生的事情是什么呢?在C ++中,因为编译后的代码在
+0和-0之间无法辨别?
A signed int reserves one bit to signify whether a number is positive or
negative. In light of this, a colleague asked me whether there existed an
int in C++ that was -0, a zero with the negative bit set. I was intrigued
by this, so I tried the following code:

#include <stdio.h>

int main(int, char**) {
int a(-0);
printf("a=%d\n", a);
if ( a==0 ) printf("a==0\n");
if ( a==-0 ) printf("a==-0\n");
return 0;
}

The output is:

a=0
a==0
a==-0

So it seems that no such number can be used in C++, and speaking of
mathematical numbers, it makes perfect sense, but what is actually going on
"behing the curtains" in C++ since the compiled code can''t discern between
+0 and -0 ?




搜索two的补码"你会看到的。这不仅仅是为

预留一点标志。


-

见到你。



Search for "two''s complement" and you''ll see. It''s not just as simple as
reserving a bit for the sign.

--
Be seeing you.


Christian Stigen Larsen写道:
Christian Stigen Larsen wrote:
签名的int保留一位来表示一个数字是正数

否定。鉴于此,一位同事问我在C ++中是否存在
int为-0,设置为负位为零。我对此很感兴趣,所以我尝试了以下代码:

#include< stdio.h>

int main(int,char **) {
int a(-0);
printf(" a =%d \ n",a);
if(a == 0)printf(" a == 0; \\ n");
if(a == - 0)printf(" a == - 0 \ n");
返回0;
}

输出是:

a = 0
a == - 0

所以似乎没有这样的数字可以在C ++中使用,并且说到数学数字,它是完全合理的,但实际上正在进行的是惊天动地。在C ++中,由于编译后的代码不能在+0和-0之间识别?
A signed int reserves one bit to signify whether a number is positive
or
negative. In light of this, a colleague asked me whether there
existed an
int in C++ that was -0, a zero with the negative bit set. I was
intrigued by this, so I tried the following code:

#include <stdio.h>

int main(int, char**) {
int a(-0);
printf("a=%d\n", a);
if ( a==0 ) printf("a==0\n");
if ( a==-0 ) printf("a==-0\n");
return 0;
}

The output is:

a=0
a==0
a==-0

So it seems that no such number can be used in C++, and speaking of
mathematical numbers, it makes perfect sense, but what is actually
going on "behing the curtains" in C++ since the compiled code can''t
discern between +0 and -0 ?




负数通常表示为2'的补码,例如一个

8位数字可以表示从-128到+127的值,包括onle one

可能的0值。它不仅仅是一个不同的符号位。


Btw:不知何故,但并非完全不相关,在浮点单位,那里

实际上经常是+0和-0,所以尽管如此1-1是0,我认为-1 + 1

导致-0。这在某些计算中很有用。



Negative numbers are usually expressed as 2''s complement, where e.g. an
8 bit number can represent values from -128 to +127, including onle one
possible value for 0. It''s not just one sign bit that is different.

Btw: Somehow, but not totally unrelated, in floating point units, there
actually often is +0 and -0, so while e.g. 1-1 is 0, I think -1+1
results in -0. This can be useful in some calculations.




" Christian Stigen Larsen" < CS ****** @ newscache.ntnu.no>在消息新闻中写道:sl ********************* @ gaupe.stud.ntnu.no ...

"Christian Stigen Larsen" <cs******@newscache.ntnu.no> wrote in message news:sl*********************@gaupe.stud.ntnu.no...
签署的int储备一点来表示一个数字是正数还是负数。鉴于此,一位同事问我在C ++中是否存在一个为-0的int,其中负位设置为零。我对此很感兴趣,所以我尝试了下面的代码:
A signed int reserves one bit to signify whether a number is positive or
negative. In light of this, a colleague asked me whether there existed an
int in C++ that was -0, a zero with the negative bit set. I was intrigued
by this, so I tried the following code:



有几种可能的整数表示形式。虽然

正数都是非常简单的二进制(这是标准所要求的b $ b),负数可以通过多种方式处理。

其中一些有负零值的可能性。然而,今天使用的最常见的表示形式称为两个补码。

两个补码没有负零,但确实有一个更多

负值可用而不是正值。也就是说,8位二进制补码

数从-128到127.


There are several singed integer representations that are possible. While
the positive numbers are all pretty straight forward binary (this is required
by the standard), the negatives can be handled in a number of ways.
Some of thse have the possiblity for a negative zero value. However, the
most common representation used today is called "two''s complement".
Two''s complement has no negative zero, but it does have one more
negative value available than positive. That is, an 8 bit twos complement
number goes from -128 to 127.


这篇关于签名int -0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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