views:

857

answers:

5

In other words does this work as expected?

int32 i = INT_MAX-1;
int64 j = i * i;

or do I need to cast the i to 64 bit first?

+16  A: 

You need to cast at least one of the operands to the multiply. At the point the multiply is being done, the system doesn't know you're planning to assign to an int64.

(Unless int64 is actually the native int type for your particular system, which seems unlikely)

Will Dean
Common trick is multiplying on 1L first instead of casting:int64 j = 1L * i * i;This works if your long has 64-bit length.
stepancheg
And if your `long` is only 32 bits, you can use 1LL on most modern compilers.
Head Geek
Or you could use INT64_C( 1) from stdint.h to make it reasonably portable (you may need to dig up a stdint.h for MS compilers, maybe from MinGW or from http://www.azillionmonkeys.com/qed/pstdint.h
Michael Burr
What are int32 and int64? Are they supposed to be like int32_t and int64_t from <stdint.h>?
Hudson
+6  A: 

It depends on what int32 and int64 are.

In brief, all integers are promoted to at least 'int' size (which may be 64 bits) before any arithmetic operations, and to the size of the larger operand for binary operators if this is of greater rank than an int.

How the result of an expression is used (whether or not it is stored to a wider type) has no bearing on the promotions of the constituent parts of the expression.

Charles Bailey
+1 for explaining instead of prescribing
David Schmitt
A: 

Given two numbers a,b and each number uses len_a and len_b bits.

Your output datatype needs at least: len_a and len_b bits.

In your above code, you have two 31 bit numbers ( because INT_MAX - 1 = 0x7FFFFFFE uses 31 bits ) and you will need to typecast one of them to int64_t because it will do a 32 bit multiply and overflow before it casts to int64_t.


The number of bits needed for fixed point multiplication:

len_output = howManyBits( a * b )
           = len_a + len_b

A quick example to show the above rule in action:

a     = 7
len_a = 3

b     = 7
len_b = 3

c = a * b
  = 49 ( decimal )
  = 0x31 ( hex )

len_c = howManyBits( 0x31 ) 
      = 6

You can write a function to count bits. Or if you just want a quick sanity check to confirm this use something like Windows Calc that will convert the number into binary form and count the bits used.

Trevor Boyd Smith
+3  A: 

The basic answer is no it will not do what you want.
But it does do what is expected.

Two things to note about mathematical operations:

  • Both operands will be the same type.
  • The resulting type will be the same as the input.

If the compiler notes a mismatch between the operands it will convert one of the operands so that both match (see http://stackoverflow.com/questions/245740/which-variables-should-i-typecast-when-doing-math-operations-in-c-c/245986#245986). Note: This is done in isolation to what happens to the result.

Martin York
A: 

See: 14. Mixed use of simple integer types and memsize types. http://www.viva64.com/art-1-2-599168895.html