溢出下溢 [英] Overflow Underflow

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

问题描述



union SignedChoice {

long with_sign;

unsigned long without_sign;

};

int main()

{

SignedChoice数据;


data.with_sign = -1;

//现在,C ++标准是否保证

//data.without_sign == MAX_ULONGINT?


//同样:


data.without_sign = MAX_ULONGINT;


// data.with_sign现在肯定是== -1?


}

还有其他有趣的事实可以给我吗?


-JKop


union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;

data.with_sign = -1;
//Right now, does the C++ Standard guarantee that
//data.without_sign == MAX_ULONGINT?

//Similarly:

data.without_sign = MAX_ULONGINT;

//Is data.with_sign now definitely == -1?

}
Is there any other interesting facts yous can give me?

-JKop

推荐答案



" JKop" < NU ** @ NULL.NULL>在消息中写道

新闻:PF ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:PF*****************@news.indigo.ie...

union SignedChoice {
long with_sign;
unsigned long without_sign;
};

int main()
{
SignedChoice数据;

data.with_sign = -1;

//现在,C ++标准是否保证
//data.without_sign == MAX_ULONGINT?

//同样:

data.without_sign = MAX_ULONGINT;

// data.with_sign现在肯定是== -1?

}

union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;

data.with_sign = -1;
//Right now, does the C++ Standard guarantee that
//data.without_sign == MAX_ULONGINT?

//Similarly:

data.without_sign = MAX_ULONGINT;

//Is data.with_sign now definitely == -1?

}




我很确定它不会。据我所知,标准确实没有指定有符号整数类型的具体表示(即

,它是两个补码,一个补充等等。什么是定义好的

是无符号整数值的溢出/下溢行为,而不是其直接等于有符号整数值的b / b
。因此,将x添加到值为
casue溢出总是可以通过从那个

结果中减去x来反转。同样,您可以通过添加减去值减去的值来撤消欠载。但这并不一定意味着-1的有符号值是
与max_whatever的无符号值相同的位表示。


-Howard



I''m pretty sure that it doesn''t. From what I understand, the standard does
not dictate the specific representation of a signed integer type (i.e,
whehter it''s two''s-complement, one''s-complement, etc.). What is wel-defined
is the overflow/underflow behavior of an unsigned integer value, not its
direct equivelance to a signed integer value. So, adding x to a value to
casue an overflow can always be reversed by then subtracting x from that
result. Likewise, you can undo an underfolw by adding back the value
subtracted. But that doesn''t neccessarily mean that s signed value of -1 is
the same bit representation as an unsigned value of max_whatever.

-Howard




Geez,我可以在一个帖子中输入多少错字?我必须得到我的手指

检查。 :-)


-Howard

Geez, how many typos can I make in one post??? I''ll have to get my fingers
checked. :-)

-Howard


2004年6月28日星期一16:27:27 GMT,JKop< NU * *@NULL.NULL>写道:
On Mon, 28 Jun 2004 16:27:27 GMT, JKop <NU**@NULL.NULL> wrote:

union SignedChoice {
long with_sign;
unsigned long without_sign;
};

int main()
{SignedChoice数据;

data.with_sign = -1;

//现在,C ++标准保证
//data.without_sign == MAX_ULONGINT?


不,你不能写一个工会的一个成员并从另一个人那里读书,

至少不是那样的(你可以使用结构相同的初始

序列),所以重点是没有实际意义。


如果你问的是-1的位模式是否保证是

全1,然后答案是否定的。 1'的补充CPU将给出(对于一个

16位int):

1111111111111110

符号幅度CPU将给出:

1000000000000001


但是,现在大多数CPU都使用2s补码来签名数字。

你能给出任何其他有趣的事实吗?我?

union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;

data.with_sign = -1;
//Right now, does the C++ Standard guarantee that
//data.without_sign == MAX_ULONGINT?
No, you can''t write to one member of a union and read from another, at
least not like that (you can with structs with identical initial
sequences), so the point is moot.

If you are asking whether the bit pattern of -1 is guaranteed to be
all 1s, then the answer is no. 1''s complement CPUs will give (for a
16-bit int):
1111111111111110
Sign magnitude CPUs will give:
1000000000000001

However, most CPUs these days do use 2s complement for signed numbers.
Is there any other interesting facts yous can give me?




根据您的兴趣:
http://www.lingolex.com/ants.htm


Tom

-

C ++常见问题解答: http://www.parashift.com / c ++ - faq-lite /

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Depending on your interests:
http://www.lingolex.com/ants.htm

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


这篇关于溢出下溢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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