表达式内的操作数是否根据以下规则提升为更大的类型?

发布于 2024-09-06 08:08:18 字数 978 浏览 12 评论 0原文

如果数值表达式包含不同数值类型的操作数(常量和变量),则操作数是否根据以下规则提升为更大的类型:

  1. 如果操作数的类型为 bytesbytecharshortushort,它们会转换为 int 类型
  2. 如果其中一个操作数是 int,则所有操作数都转换为 int
  3. 如果表达式还包含 uintint 类型的操作数,则所有操作数都转换为long
  4. 如果其中一个操作数为 long,则
  5. 当表达式包含 ulong 类型的操作数时, 所有操作数都将转换为 longlong,则操作数将转换为 float
  6. 如果其中一个操作数为 float,则所有操作数都将转换为 float code>
  7. 如果其中一个操作数为 double,则所有操作数都会转换为 double

假设数值表达式包含不同类型的操作数,所有操作数首先会转换为单一数值类型,然后运行时才会尝试计算结果?例如,如果变量 b1b2byte 类型,而 i1int< /code> 类型,在计算 (b1+b2) 之前将 b1 和 b2 get 转换为 int:

int i2=(b1+b2)+i1

If numeric expression contains operands (constants and variables) of different numeric types, are operands promoted to larger types according to the following rules:

  1. if operands are of types byte, sbyte, char, short, ushort, they get converted to int type
  2. If one of the operands is int, then all operands are converted to int
  3. if expression also contains operands of types uint and int, then all operands are converted to long
  4. If one of operands is long, then all operands are converted to long
  5. if expression contains operands of type ulong and long, then operands are converted to float
  6. If one of the operands is float, then all operands are converted to float
  7. if one of operands is double, then all operands are converted to double

Assuming numeric expressions contains operands of different types, will all operands first get converted to a single numeric type, and only then will the runtime try to compute the result? For example, if variables b1 and b2 are of byte type, while i1 is of int type, will b1 and b2 get converted to int prior to computing (b1+b2):

int i2=(b1+b2)+i1

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

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

发布评论

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

评论(2

陌路终见情 2024-09-13 08:08:18

括号的优先级高于 +,因此转换通常会在添加 b1b2 之后进行。但是,+ 运算符对于 byte 没有重载,因此必须首先将 byte 提升为 int

进一步阅读:

The parentheses are of higher precedence than +, so the conversion would normally take place after b1 and b2 have been added. However, the + operator does not have an overload for bytes, so the bytes must first be promoted to ints.

Further reading:

紅太極 2024-09-13 08:08:18

你的规则有一些真理元素,但在技术上并不精确。

以下是 C# 语言规范的相关摘录

7.2.6.2 二进制数字升级< /a>

预定义 +*/、< 的操作数发生二进制数字提升代码>%、<代码>&、<代码>|、<代码>^、<代码>==、<代码>! =、><>=<= 二元运算符。二进制数字提升隐式地将两个操作数转换为公共类型,在非关系运算符的情况下,该类型也成为操作的结果类型。二进制数字提升包括应用以下规则,按照它们在此处出现的顺序:

  • 如果任一操作数的类型为 decimal,则另一个操作数将转换为 decimal 类型,或者如果另一个操作数的类型为 ,则会发生编译时错误>浮点双精度
  • 否则,如果任一操作数的类型为 double,则另一个操作数将转换为 double 类型。
  • 否则,如果任一操作数的类型为 float,则另一个操作数将转换为 float 类型。
  • 否则,如果任一操作数为 ulong 类型,则另一个操作数将转换为 ulong 类型,或者如果另一个操作数为 ulong 类型,则会发生编译时错误sbyteshortintlong
  • 否则,如果任一操作数的类型为 long,则另一个操作数将转换为 long 类型。
  • 否则,如果其中一个操作数的类型为 uint,而另一个操作数的类型为 sbyteshortint< /code>,两个操作数都转换为 long 类型。
  • 否则,如果任一操作数的类型为 uint,则另一个操作数将转换为 uint 类型。
  • 否则,两个操作数都将转换为 int 类型。



int i2=(b1+b2)+i1

根据上述规范,是的,字节 b1, b2 会被二进制数字提升为 int 为二元运算符+

Your rules have some elements of truths, but is technically imprecise.

Here are the relevant excerpts from the C# Language Specification

7.2.6.2 Binary numeric promotions

Binary numeric promotion occurs for the operands of the predefined +, , *, /, %, &, |, ^, ==, !=, >, <, >=, and <= binary operators. Binary numeric promotion implicitly converts both operands to a common type which, in case of the non-relational operators, also becomes the result type of the operation. Binary numeric promotion consists of applying the following rules, in the order they appear here:

  • If either operand is of type decimal, the other operand is converted to type decimal, or a compile-time error occurs if the other operand is of type float or double.
  • Otherwise, if either operand is of type double, the other operand is converted to type double.
  • Otherwise, if either operand is of type float, the other operand is converted to type float.
  • Otherwise, if either operand is of type ulong, the other operand is converted to type ulong, or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.
  • Otherwise, if either operand is of type long, the other operand is converted to type long.
  • Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
  • Otherwise, if either operand is of type uint, the other operand is converted to type uint.
  • Otherwise, both operands are converted to type int.

int i2=(b1+b2)+i1

As per the specification above, yes, byte b1, b2 are subject to binary numeric promotion to int for the binary operator +.

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