Are operands inside an expression promoted to larger types according to the following rules?
-
28-09-2019 - |
Question
If numeric expression contains operands (constants and variables) of different numeric types, are operands promoted to larger types according to the following rules:
- if operands are of types
byte
,sbyte
,char
,short
,ushort
, they get converted toint
type - If one of the operands is
int
, then all operands are converted toint
- if expression also contains operands of types
uint
andint
, then all operands are converted tolong
- If one of operands is
long
, then all operands are converted tolong
- if expression contains operands of type
ulong
andlong
, then operands are converted tofloat
- If one of the operands is
float
, then all operands are converted tofloat
- if one of operands is
double
, then all operands are converted todouble
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
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 byte
s, so the byte
s must first be promoted to int
s.
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 typedecimal
, or a compile-time error occurs if the other operand is of typefloat
ordouble
.- Otherwise, if either operand is of type
double
, the other operand is converted to typedouble
.- Otherwise, if either operand is of type
float
, the other operand is converted to typefloat
.- Otherwise, if either operand is of type
ulong
, the other operand is converted to typeulong
, or a compile-time error occurs if the other operand is of typesbyte
,short
,int
, orlong
.- Otherwise, if either operand is of type
long
, the other operand is converted to typelong
.- Otherwise, if either operand is of type
uint
and the other operand is of typesbyte
,short
, orint
, both operands are converted to typelong
.- Otherwise, if either operand is of type
uint
, the other operand is converted to typeuint
.- 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 +
.