Does the following arithmetic sum need?

short a;
short b;
short x;
int c = (int)a + (int)b * (int)x;

Can I refuse a cast (int) in this case? that is, when the compiler performs multiplication and addition, does it work with int intermediate variables or with short intermediate variables?

Edit: what about other types?

int32 a;
int32 b;
int32 x;
int64 c = (int64)a + (int64)b * (int64)x;

Edit 2: It seems to me that

int64 c = a + b*x 

may overflow since b * x is computed using int arithmetic. Safer expression:

int64 c = a + (int64)b * (int64)x;
+4
source share
2 answers

For the case short, ghosts are not needed, since the values ​​are shortimplicitly advanced to intin arithmetic terms.

, , int32 int64 , , , int 64 . , * , :

int64 c = a + (int64)b * x;

b int64, x int64, * , , , . a int64 , '+' . , . , c.

+4

++ (5.7 )

1 + - . .

, int int.

.

short int, int , .:)

int32 a;
int32 b;
int32 x;
int64 c = (int64)a + (int64)b * (int64)x;

int64 c = a + (int64)b * x;

, .

+1

All Articles