算术运算期间的数据类型提升:-1 < (unsinged int) 1 == false

发布于 2024-09-27 00:16:49 字数 911 浏览 6 评论 0 原文

main()  {   
  if ( -1 < (unsigned char) 1 )
    printf("less than");
  else        
    printf("NOT less than");
} 

打印小于。因为,(unsigned char) 1 被转换为 (signed char) 1,然后: (signed) -1 (有符号)1,因此输出小于

但是,如果我将上面的代码更改为 if ( (-1 < (unsigned int) 1 )

则输出不小于

因此,很明显,当我将 unsigned char 更改为 unsigned int 时:

  • (signed) -1 转换为 unsigned int [发生的情况正好相反],
  • 因为 -1 存储为 2 的补码 1;位模式被评估为 255(可能) )
  • 因此 255 < 1 将计算为 false,
  • 即使您用 int a = -1; 代替 '-1' 也会执行相同的结果

问题:

  1. 在有符号和无符号期间。算术...如何确定有符号是否会转换为无符号,反之亦然。

  2. 。 unsigned char 和 char 之间的算术转换是否不同:显然 unsigned 转换为有符号,unsigned int 和 int :显然有符号转换为无符号

PS:我知道这不依赖于编译器..所以不要说它是。

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 is less 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 result

Questions:

  1. during signed and unsigned arithmetic...how to be sure if signed will be converted to unsigned or vice versa.

  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-10-04 00:16:49

规则如下:

6.3.1.8 常用算术转换

...

否则,将对两个操作数执行整数提升。然后
以下规则适用于提升的操作数:

  1. 如果两个操作数具有相同的类型,则不需要进一步转换。
  2. 否则,如果两个操作数都具有有符号整数类型或都具有无符号整数类型,则具有较小整数转换等级的类型的操作数将转换为具有较大等级的操作数的类型。
  3. 否则,如果无符号整数类型的操作数的等级大于或等于另一个操作数类型的等级,则有符号整数类型的操作数将转换为无符号整数类型的操作数的类型。< /里>
  4. 否则,如果有符号整型操作数的类型可以表示无符号整型操作数类型的所有值,则将无符号整型操作数转换为有符号整型操作数的类型类型。
  5. 否则,两个操作数都会转换为与有符号整数类型操作数的类型相对应的无符号整数类型。

规则如下:

  • -1 -1 -1 -1 -1 -1 -1 (unsigned char) 1

首先,两个操作数都转换为 int(因为 int 可以表示 unsigned char 的所有值)。然后对这些有符号类型进行比较。然后使用规则 1。比较成功。

  • <代码>-1 < (unsigned int) 1

int 不能表示 unsigned int 的所有值,因此使用规则 3,并将有符号整数转换为无符号整数 (UINT_MAX - 1)。现在比较失败了。

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:

  1. If both operands have the same type, then no further conversion is needed.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

原野 2024-10-04 00:16:49

这是由于整数促销。两个参数都可以表示为 int,因此它们都会转换为 int。

ISO C 6.3.1.1,第 2 段:

如果int可以表示原始类型的所有值,则将该值转换为int;
否则,它被转换为无符号整型。这些被称为整数
促销。48) 所有其他类型都不会因整数促销而改变。

This is due to integer promotions. Both arguments can be represented as an int, so they are converted to an int.

ISO C 6.3.1.1, paragraph 2:

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 integer
promotions.48) All other types are unchanged by the integer promotions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文