无符号和带符号的比较 [英] Unsigned and signed comparison

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

问题描述

这里是非常简单的代码,

Here is very simple code,

#include <iostream>
using namespace std;
int main() {
    unsigned int u=10;
    int i;
    int count=0;
    for (i=-1;i<=u;i++){
        count++;
    }
    cout<<count<<"\n";
    return 0;
}

count的值为0.为什么?

The value of count is 0. Why?

推荐答案

< = 的两个操作数必须提升为同一类型。

Both operands of <= have to be promoted to the same type.

显然他们被提升为 unsigned int (我没有在我面前的标准规则,我会查找它在一秒钟)。由于(unsigned int)( - 1)< = u 为假,循环不会执行。

Evidently they are promoted to unsigned int (I don't have the rule from the standard in front of me, I'll look it up in a second). Since (unsigned int)(-1) <= u is false, the loop never executes.

规则在标准第5节(expr)第10段中找到,其中规定了(我强调了在这里应用的规则):

The rule is found in section 5 (expr) of the standard, paragraph 10, which states (I've highlighted the rule which applies here):


许多期望算术或枚举类型的操作数以类似方式导致转换和yield结果类型的二进制运算符。目的是产生一个共同的类型,这也是结果的类型。
这种模式称为通常的算术转换,定义如下:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:


  • 如果任一操作数是scoped枚举类型7.2),不执行转换;如果另一个操作数不具有相同的类型,则表达式是错误的。

  • 如果任一操作数的类型为long double,则另一个将被转换为long double。

  • 否则,如果任一操作数为double,则另一个将转换为double。

  • 否则,如果任一操作数为float,

  • 否则,积分促销(4.5)将在两个操作数上执行。 60然后,以下
    规则应用于提升的操作数:

  • 如果两个操作数具有相同的类型,则不需要进一步的转换。

  • 否则,如果两个操作数都具有带符号整数类型,或者两者都具有无符号整数类型,则具有较小整数转换rank类型的操作数将转换为具有更高排序的操作数的类型。

  • 否则,如果具有无符号整数类型的操作数的rank大于或等于另一个操作数的类型的rank,则带有有符号整数类型的操作数将转换为具有无符号整数类型的操作数的类型整数类型

  • 否则,如果带有有符号整数类型的操作数的类型可以表示具有无符号整数类型的操作数的类型的所有值,无符号整数类型应转换为带有有符号整数类型的操作数的类型。

  • 否则,两个操作数都应转换为与带符号整数的操作数类型相对应的无符号整数类型类型。

  • If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
  • If either operand is of type long double, the other shall be converted to long double.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.
  • Otherwise, the integral promotions (4.5) shall be performed on both operands. 60 Then the following rules shall be applied to the promoted operands:
  • If both operands have the same type, 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 shall be converted to the type of the operand with greater rank.
  • Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be 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, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.
  • Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

这篇关于无符号和带符号的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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