1

I'm having troubles understanding this line of code that I see with every Arduino EEPROM tutorial.

int saveLocation = 4095;
Wire.write((int)(saveLocation >> 8));

Question 1: Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

According to the wire library documentation, you can use wire to send the following:

Wire.write(value) // value to send as a single byte
Wire.write(String) // a string to send as a series of bytes
Wire.write(data, length) // an array of data to send as bytes, the number of bytes to transmit.

So my variable, int saveLocation = 4095 is not a string or array, so it must be a considered as a 'value', which means it's being sent as 1 byte.

Question 2: If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?
Question 3: If saveLocation is already an integer, why would cast it to int?
Question 4: Since int is two bytes, does wire.write only send out the lower byte?

Thanks in advance for the help.

1
  • you're missing 1/2 of the EEPROM addressing code
    – jsotola
    Apr 19, 2022 at 0:23

2 Answers 2

2

You are right about being confused. This casting doesn't make any sense.

Question 1: Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

Yes, the bitshift happens before the casting because of the parentheses.

Question 2: If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?

You wouldn't. It is cast to uint8_t (aka byte) anyway by the parameter type.

Question 3: If saveLocation is already an integer, why would cast it to int?

Doesn't make sense to me either. You don't need to. Though it also doesn't hurt here (same goes for question 2)

Question 4: Since int is two bytes, does wire.write only send out the lower byte?

Yes, only the lower byte. The function parameter is of type uint8_t, which is exactly one byte. Thats the reason for the bit shifting. By shifting the value right by 8 you effectively shift the high byte to the low byte position.

2
  • 1
    Dude I thought I was nuts! But what I don't understand is if you search online for code to address EEPROM chips, they all use that line for sending data. I thought that surely I had to be missing something because EVERY tutorial has that same stupid line of code. Thank you for helping me clear this up! Apr 18, 2022 at 22:26
  • 2
    Often when an example code is written it gets copied over and over throughout the internet.
    – chrisl
    Apr 18, 2022 at 22:35
1

Question 1: Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

Yes. What's in the brackets is evaluated then the result cast to int.

Question 2: If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?

You wouldn't.

Question 3: If saveLocation is already an integer, why would cast it to int?

You wouldn't

Question 4: Since int is two bytes, does wire.write only send out the lower byte?

Yes, just the lowest 8 bits.

The cast to an integer is completely redundant here. It's not performing anything other than making the code harder to understand. You can remove it and it won't make any difference.

In fact the presence of the int cast triggers an overloaded function:

inline size_t write(int n) { return write((uint8_t)n); }

So it just re-casts it to an unsigned 8-bit integer (byte) anyway.

1
  • Thank you Majenko as well. I don't understand why all the code online for EEPROM chips use this line of code, it's beyond confusing. Apr 18, 2022 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.