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
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 intint was a long but it isn't. intint is int.
0xFF02 * 512 is 1024 because the high order word is lost.