Question

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
Was it helpful?

Solution

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:

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top