Unexpected behavior of implicit type conversion

Hi Everyone,

I have been having an issue with the following code when saving Data to EEPROM, I may not understand proeprly how implicit type casting works or maybe is some other thing, so I would like to know exactly why the following behaviour.

/*When doing the following it works perfectly and the output is as expected 0x3664*/
uint16_t word = 0x3664;
uint8_t low_byte  = word >> 8;
uint8_t high_byte = word;
EEPROM.write(0x10,low_byte);
EEPROM.write(0x11,high_byte);
Serial.print(F("16 bits Word -> "));
Serial.println(word,HEX);
p = EEPROM.read(0x10)<<8;
p += EEPROM.read(0x11);
Serial.print(F("Read Word ->  "));
Serial.println(p,HEX);
/*However when doing the impicit typecasting as parameter of read (), it does not work and I just get 
 the low byte*/
uint16_t word = 0x3664;
EEPROM.write(0x10,(uint8_t)word>>8);
EEPROM.write(0x11,(uint8_t)word);
Serial.print(F("16 bits Word -> "));
Serial.println(word,HEX);
p = EEPROM.read(0x10)<<8;
p += EEPROM.read(0x11);
Serial.print(F("Read Word ->  "));
Serial.println(p,HEX);

I hope I have made myself clear enoguh, thank you very much for your help.

Cheers !

In the second example what is “i”? Best to post full code and not snippets.

You do know “word” is a data type in the Arduino IDE and is probably not a good choice for variable names. word - Arduino Reference

EEPROM.write(0x10,(uint8_t)word>>8);

you first cast 'word' to 8-bit, and then you bitshift, this should be the other way around

EEPROM.write(0x10,(uint8_t) (word>>8) );

Mind you i think the write function only takes a byte as an argument so casting is should not be required.

    write(0x10,(uint8_t)(word>>8));
    write(0x11,(uint8_t)(word & 0xFF));

Or, simply use the put and get methods.

Thanks so much for your help.