# Multiplying 2 ints

I have a interesting situation, I start off with 2 bytes, use the word() function to make a int, then multiply that word by 512 in a long, but it seems to totally ignore the high byte

for example

``````void setup()
{
byte HI = 0xFF;
byte LO = 0x02;
word com = word(HI,LO);
unsigned long exp = (com * 512);
Serial.println(exp);
}
``````

result is 1024, not 33424384 as I expected, so what am I missing here?

PS: its also happening with the high byte being any value, not just 0xFF

Does com have the value you expect?

yes com spits out 65282 if I print it to the serial monitor

What if you multiply by 512UL, instead? I don't have an Arduino here to test with.

I dont have one on hand at the moment either, but I will try when I get home this evening

Osgeld: result is 1024, not 33424384 as I expected, so what am I missing here?

If you look at the hex of 33424384: 0x1FE0400, you'll notice the lower order word 0x0400 is equivalent to 1024. That means that com * 512 is being done as integer division and the higher order word is lost before it gets casted to an unsigned long. Using the UL against the 512 as PaulS said will force unsigned long multiplication, which won't drop the higher order word.

I got caught by this too. http://arduino.cc/forum/index.php/topic,121141.0.html I thought the intermediate result of int*int was a long but it isn't. int*int is int. 0xFF02 * 512 is 1024 because the high order word is lost.

Pete

512UL totally fixed it thanks Paul and everyone

Good timing, I had this exact same problem last night

Thanks

Duane B