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

Can I dispense with the (int) casts in this case? i.e. when compiler performs the multiply and add, does it work with int intermediate variables, or with short intermediate variables?

Edit: what about for 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 

can overflow, because b*x is calculated using int arithmetic. The safer expression is:

int64 c = a + (int64)b * (int64)x;
Alex
  • 163
  • 7
Jacko
  • 12,665
  • 18
  • 75
  • 126
  • 1
    See [Why must a short be converted to an int before arithmetic operations in C and C++?](http://stackoverflow.com/q/24371868/1708801) – Shafik Yaghmour Jun 04 '15 at 20:01

2 Answers2

4

For the case of short, the casts are unnecessary because short values are implicitly promoted to int in arithmetic expressions.

For the other example, assuming int32 and int64 are what they seem to be, the cast is necessary if int is smaller than 64 bits. Technically, a single cast on one the the operands to * would suffice, such as :

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

b is first converted to int64, x is also converted to int64 because * converts the other operand if it has a lower rank, then multiplication is performed, which cannot overflow. a is then converted to int64 for the same reason, because the other operand to '+' has a higher rank. The addition is performed, it cannot overflow either. Finally the result is stored to c.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • So, is cast performed before multiply? – Jacko Jun 04 '15 at 20:23
  • @Jacko: of course, because casts, like all unary operators have higher precedence than binary operators. – chqrlie Jun 04 '15 at 20:30
  • Is this cast necessary for 64 bit builds ? – Jacko Jun 05 '15 at 03:20
  • @Jacko: 64 bit build is not precise enough: the cast is not necessary if the `int` type is smaller than 64 bits. Most 64 bits environments still keep the `int` type as 32 bits but make pointers 64 bits. If the `int` is 32 bits, the cast is necessary. If it is 64 bits, the cast does not do anything, so you should always write it. – chqrlie Jun 05 '15 at 20:03
1

From the C++ Standard (5.7 Additive operators)

1 The additive operators + and - group left-to-right. The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.

the usual arithmetic conversions include the integral promotion when objects with rank less than the rank of type int are converted to type int.

The same is valid for multiplicative operators.

In your example operands of type short are automatically converted to type int and the compiler performs the operations with operands of type int Thus using of the casting does not make a great sense.:)

As for this code snippet

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

then you indeed should cast at least one operand

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

if you want that there would not be overflow.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335