〜运算符返回有符号值 [英] ~ operator returns signed value

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

问题描述

当我这样做时,gcc显然返回-1

~(unsigned short)0


如果是unsigned int,则不是这种情况。用来代替unsigned

short。


C标准对此有何评价? (参考标准

表示赞赏。)


谢谢。


Reza。

解决方案

RezaRob写道:


>

gcc当我这样做时显然返回-1

~(unsigned short)0


如果是unsigned int,则不是这种情况。用来代替unsigned

short。


C标准对此有何评价? (参考标准

表示赞赏。)



如果INT_MAX大于或等于USHRT_MAX,

然后(〜(无符号短)0)意味着与

(〜(int)(无符号短)0)相同的东西。


ISO / IEC 9899:1990

6.2.1算术操作数

6.2.1.1字符和整数

char,short int或int bit-字段,

或其签名或未签名的变体,

或枚举类型,可以在表达式中使用

无论int还是unsigned int可以使用。

如果int可以表示原始类型的所有值,则

将值转换为int;否则,

它被转换为unsigned int。

这些被称为积分运动。

所有其他算术类型由积分保持不变促销。


-

pete


pete写道:


>

RezaRob写道:



gcc显然返回-1当我这样做

~(unsigned short)0


如果是unsigned int,则不是这种情况。用来代替unsigned

short。


C标准对此有何评价? (参考标准

表示赞赏。)



如果INT_MAX大于或等于USHRT_MAX,

然后(〜(无符号短)0)意味着与

(〜(int)(无符号短)0)相同的东西。


ISO / IEC 9899:1990

6.2.1算术操作数

6.2.1.1字符和整数

char,short int或int bit-字段,

或其签名或未签名的变体,

或枚举类型,可以在表达式中使用

无论int还是unsigned int可以使用。

如果int可以表示原始类型的所有值,则

将值转换为int;否则,

它被转换为unsigned int。

这些被称为积分运动。



应该是这些被称为整体促销。


所有其他算术类型整体促销活动保持不变。



-

pete


9月23日下午3:47 ,pete< pfil ... @ mindspring.comwrote:


如果INT_MAX大于或等于USHRT_MAX,则

然后(〜 (unsigned short)0)与

(〜(int)(unsigned short)0)的含义相同。


ISO / IEC 9899:1990

6.2.1算术操作数

6.2.1.1字符和整数

[...]



Pete,我真的很感激。


Reza。


gcc is clearly returning -1 when I do this
~ (unsigned short) 0

which is not the case if "unsigned int" is used instead of "unsigned
short".

What does the C standard say about that? (reference to the standard
is appreciated.)

Thanks.

Reza.

解决方案

RezaRob wrote:

>
gcc is clearly returning -1 when I do this
~ (unsigned short) 0

which is not the case if "unsigned int" is used instead of "unsigned
short".

What does the C standard say about that? (reference to the standard
is appreciated.)

If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).

ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
A char, a short int, or an int bit-field,
or their signed or unsigned varieties,
or an enumeration type, may be used in an expression
wherever an int or unsigned int may be used.
If an int can represent all values of the original type,
the value is converted to an int; otherwise,
it is converted to an unsigned int.
These are called the integral romotions.
All other arithmetic types are unchanged by the integral promotions.

--
pete


pete wrote:

>
RezaRob wrote:


gcc is clearly returning -1 when I do this
~ (unsigned short) 0

which is not the case if "unsigned int" is used instead of "unsigned
short".

What does the C standard say about that? (reference to the standard
is appreciated.)


If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).

ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
A char, a short int, or an int bit-field,
or their signed or unsigned varieties,
or an enumeration type, may be used in an expression
wherever an int or unsigned int may be used.
If an int can represent all values of the original type,
the value is converted to an int; otherwise,
it is converted to an unsigned int.
These are called the integral romotions.

That should be "These are called the integral promotions."

All other arithmetic types are unchanged by the integral promotions.

--
pete


On Sep 23, 3:47 pm, pete <pfil...@mindspring.comwrote:

If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).

ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
[...]

Pete, I really appreciate this.

Reza.


这篇关于〜运算符返回有符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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