unsigned to signed integer convesion [英] unsigned to signed integer convesion

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

问题描述

如何完成未签名的签名integet转换?

例如:


unsigned int ui = 100;

int si = ui;


如果si是什么价值?打印?如何转换




Thanx提前提供任何帮助....

How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?

Thanx for any help in advance ....

推荐答案

ju ********** @ yahoo .co.in 写道:
如何完成无符号签名的integet转换?
例如:

unsigned int ui = 100;
int si = ui;

如果si是什么值?打印?这个转换是如何完成的?
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?




si将为100,ui的值转换为si的类型(签名

int)并且结果存储在si中。如果ui的值大于

INT_MAX(在这种情况下不是这样),则在此转换过程中会发生溢出,从而导致未定义的行为。


Robert Gamble



si will be 100, the value of ui is converted to the type of si (signed
int) and the result is stored in si. If the value of ui is larger than
INT_MAX (which it isn''t in this case), overflow will occur during this
conversion process resulting in undefined behavior.

Robert Gamble


ju ********** @ yahoo.co.in 写道:
如何完成无符号签名的integet转换?


C99 6.3.1.3:


当整数类型的值转换为另一个整数时

类型其他比_Bool,如果值可以用新的

类型表示,它是不变的。


否则,如果新类型是无符号的,则值为转换为

重复增加或减去一个可以用新类型表示的最大值

,直到该值在

中新型的范围。 (脚注:规则描述算术

的数学值,而不是给定类型的

表达式的值。)


否则,新类型已签名,其值不能代表其中的b $ b;结果是实现定义的,或者是b $ b,实现定义的信号被引发。

例如:

unsigned int ui = 100;
int si = ui;

如果si是什么值?打印?这个转换是如何完成的?
How the unsigned to signed integet conversion is done ?
C99 6.3.1.3:

When a value with integer type is converted to another integer
type other than _Bool, if the value can be represented by the new
type, it is unchanged.

Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value
that can be represented in the new type until the value is in the
range of the new type. (Footnote: The rules describe arithmetic
on the mathematical value, not the value of a given type of
expression.)

Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined or
an implementation-defined signal is raised.
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?




因为unsigned int和int都保证能够代表

值100 ,si的值为100.


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Since both unsigned int and int are guaranteed to be able to represent
the value 100, the value of si will be 100.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


" Robert Gamble" < RG ******* @ gmail.com>写道:
"Robert Gamble" <rg*******@gmail.com> writes:
ju ********** @ yahoo。 co.in 写道:
如何完成无符号签名的integet转换?
例如:

unsigned int ui = 100;
int si = ui;

如果si是什么值?打印?这个转换是如何完成的?
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?



si将为100,ui的值被转换为si的类型(签名
int),结果是存储在si。如果ui的值大于
INT_MAX(在这种情况下不是这样),则在此转换过程中将发生溢出,从而导致未定义的行为。



si will be 100, the value of ui is converted to the type of si (signed
int) and the result is stored in si. If the value of ui is larger than
INT_MAX (which it isn''t in this case), overflow will occur during this
conversion process resulting in undefined behavior.




不,转换规则与

算术运算符的规则不同。


将有符号或无符号类型转换为一个无符号类型,

结果是明确定义的(它包裹着)。


将有符号或无符号类型转换为无符号类型时,如果

结果无法表示,结果是

实现定义或实现定义的信号被提出。

没有鼻子允许恶魔。


在此主题中查看我对标准定义的其他回复。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~ kst>

圣地亚哥超级计算机中心< * GT; < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



No, the rules for conversions are different from the rules for
arithmetic operators.

When converting a signed or unsigned type to an unsigned type,
the result is well-defined (it wraps around).

When converting a signed or unsigned type to an unsigned type, if the
result can''t be represented, either the result is
implementation-defined or an implementation-defined signal is raised.
No nasal demons are allowed.

See my other response in this thread for the standard''s definition.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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