views:

50

answers:

2

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
+1  A: 

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

polygenelubricants
+1  A: 

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:

HTH,
Kent

Kent Boogaart
I haven't yet covered operator overloading, so my question may be way off...but what has precedence and operator overloading have to do with whether b1 and b2 will be promoted to int before or after b1+b2?
flockofcode
If there was an overload of the + operator that took bytes as operands, there would be no need to promote b1 and b2 to int prior to the parenthesized addition. Imagine you had `(i1 + i2) + l`, where `i1` and `i2` are `int`s and `l` is a `long`. The parenthesized addition would yield an `int`, which would then be promoted to `long` in order to add it to `l`.
Kent Boogaart
So int has + operator overload that can take int as operands, and similarly, types float, double, ulong, decimal also have + operator overloaded, while sbyte, byte and short don't have operator + overloaded?
flockofcode
@flock: yes, see the first link (inside the first paragraph) in my answer for a list of + overloads.
Kent Boogaart
thank you very much for your help
flockofcode