问题描述
当我这样做时,gcc显然返回-1~(unsigned short)0
如果是unsigned int,则不是这种情况。用来代替unsigned
short。
C标准对此有何评价? (参考标准
表示赞赏。)
谢谢。
Reza。
如果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
如果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,我真的很感激。
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.
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
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."
--
pete
Pete, I really appreciate this.
Reza.
这篇关于〜运算符返回有符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!