C#

Copy Code

sum = (Int16)(num1 + num2); product = (Int16)(num1 * num2);

will work. (unless the result is greater than

`Int16.MaxValue`

(32.768))The arithmetic operators (+, −, *, /, %) are defined for all numeric types

*except*the 8- and 16-bit integral types. Int16 is a 16 bit integral. So if you multiply two Int16 (or Int8 etc) a 32bit arithmetic operator is used. And that is why the result is a Int32.

Operator for the "biggest" datatype is always used. If you add a 16Bit value and a 32bit value it will return a 32bit value. If you multiply a 32bit value and a 64bit value it will return a 64bit value.

some examples:

Copy Code

Int16 + Int32 = Int32 Int16 + Int64 = Int64 Int32 * Int32 = Int32 Int32 * Int64 = Int64