在算术运算数据类型优惠:-1 LT; (unsinged INT)1 ==假 [英] Data type promotions during arithmetic operations: -1 < (unsinged int) 1 == false
问题描述
的main(){
如果(-1≤(无符号字符)1)
输出(小于);
其他
的printf(不低于);
}
打印比
少。因为,(unsigned char型)1
的转换为的(符号字符)1
,然后:(签字)-1 LT; (签字)1
,从而输出小于
。
但是,如果我更改上述code 到 IF((-1≤(unsigned int类型)1)
然后输出不小于
。
所以,很明显,当我改变unsigned char型到unsigned int类型:
- (签字)-1转换为无符号整型正好相反发生]
- -1以来存储为1 2的恭维;的位模式被评估为255(可能)
- 因此255 LT; 1将评估为false,否则将被执行。
- 即使你替换
int类型的= -1;
代替'-1'相同的结果
问题:
-
在符号和无符号运算......如果签约将被转换为无或如何确保反之亦然。
-
为什么转换为unsigned char型和char之间的算术不同:显然无符号转换为有符号和unsigned int和int:显然签的是转换器的无符号
PS:我知道这是不是编译器dependent..so不说这是
的规则如下:
6.3.1.8通常的算术转换
...
另外,整数促销活动是在两个操作数执行。然后,
以下规则应用于提升操作数:
- 如果两个操作数具有相同的类型,则不需要进一步的转换。
- 否则,如果两个操作数有符号整数类型或两者都有的无符号整数类型,具有较小的整数转换等级的类型将被转换为操作数的类型有更大的等级。
- 否则,如果具有无符号整型操作数的秩大于或等于另一个操作数的类型的秩,然后用符号整型操作数被转换成无符号整型操作数的类型。
- 否则,如果有符号整数类型的操作数的类型可以重新present所有无符号整型操作数的类型的值,然后用无符号整型的操作数转换为的类型操作有符号整数类型。
- 否则,两个操作数转换为对应于与符号整型操作数的类型的无符号整数类型。
块引用>该规则,那么工作方式如下:
-1 LT; (unsigned char型)1
首先两个操作数转换为整数(因为int可以重新present unsigned char型的所有值)。然后比较这些符号类型的。那么规则1被使用。比较成功。
-1 LT; (无符号整数)1
所以第3条规则的使用量和符号整数转换为无符号整数一个int不能重新present一个unsigned int的所有值(UINT_MAX - 1)。比较现在失败。
main() { if ( -1 < (unsigned char) 1 ) printf("less than"); else printf("NOT less than"); }
Prints
less than
. Because,(unsigned char) 1
is converted to(signed char) 1
and then:(signed) -1 < (signed) 1
, thus output isless than
.But if I change the above code to
if ( (-1 < (unsigned int) 1 )
then the output is
NOT less than
.So it's obvious that when I change unsigned char to unsigned int:
- (signed) -1 is converted to unsigned int [exactly opposite is happening]
- since -1 is stored as 2's compliment of 1; the bit-pattern is evaluated as 255 (probably)
- thus 255 < 1 will evaluate to false and else will execute.
- even if you substitute
int a = -1;
in place of '-1' same resultQuestions:
during signed and unsigned arithmetic...how to be sure if signed will be converted to unsigned or vice versa.
why is conversion different for arithmetic between unsigned char and char : apparently unsigned is converted to signed and unsigned int and int : apparently signed is converter to unsigned
PS: I know this is not compiler dependent..so don't say it is.
解决方案The rules are as follows:
6.3.1.8 Usual arithmetic conversions
...
Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
- Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
The rules then work as follows:
-1 < (unsigned char) 1
First both operands are converted to ints (because an int can represent all values of unsigned char). Then the comparison is made on these signed types. Rule 1 is then used. The comparison succeeds.
-1 < (unsigned int) 1
An int cannot represent all the values of an unsigned int so rule 3 is used and the signed integer is converted to an unsigned integer (UINT_MAX - 1). The comparison now fails.
这篇关于在算术运算数据类型优惠:-1 LT; (unsinged INT)1 ==假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!