try to multiply two uint32_t into a uint64_t seems not to work

Status
Not open for further replies.

Theremingenieur

Senior Member+
I wanted to create a function which multiplies a uint32_t with a uint16_t and with another uint16_t which would give a uint64_t result. That did not work on my Teensy 3.1.
Printing a uint64_t with Serial.print() gives a compiler error, obviously there is a prototype missing :
Code:
  Serial.begin(57600);
  delay(1000);
  uint64_t result = (uint32_t)0x6459D01A * (uint16_t)0x803B;
  Serial.print(result);
does not compile, it says "error: call of overloaded 'print(uint64_t&)' is ambiguous".

So, I tried to divide my resulting uint64_t up into two uint32_t (upper and lower):
Code:
   Serial.begin(57600);
  delay(1000);
  uint64_t result = (uint32_t)0x6459D01A * (uint16_t)0x803B;
  uint32_t lower = result & 0xFFFFFFFF;
  uint32_t higher = (uint64_t)result >> 32;
  Serial.print(higher, HEX);
  Serial.print(" + ");
  Serial.print(lower, HEX);

The result is : "0 + 8BFF5FE". That means that the upper half of the uint64_t is either not calculated or it is truncated.

What am I doing wrong???

Thank you in advance for your help!
 
Last edited:
Try this:

Code:
uint64_t result = (uint64_t)0x6459D01A * (uint16_t)0x803B;

The compiler will still implement this as a 32x32 multiply, but at least one of the inputs needs to be cast to 64 bits to prevent the compiler from defaulting to only a 32 bit result. I don't know why assigning the result to a 64 bit variable isn't enough, but apparently it isn't. I know casting one of the inputs to 64 bits works, because I used it in numerous places in the audio lib.
 
Status
Not open for further replies.
Back
Top