Skip to main content
corrected, the c++ language has no specified order
Source Link
Jot
  • 3.3k
  • 1
  • 14
  • 21

Yes and yes, although the compiler might give a warning@PetioPetrov, perhaps a type casting or conversion is needed.

Arduino has macros to make it easier: highByte, lowByte, and word.

You don't even have to split and combine bytes and words. The EEPROM.put and EEPROM.get can write and read two bytes if you want to. It's all documented in the reference of EEPROM.

There is a lot going on in this piece of code:

int16_t GyX;
GyX=Wire.read()<<8|Wire.read();

Shifting a single byte 8 bits to the left would be wrong. However, the Wire.read returns an integer and the GyX is an integer. Therefor the compiler uses integers for everything and shifting 8 bits to the left is valid.
TheAt this moment with the current Arduino and avr-gcc compiler version, it will call the functions from left to right, soWire.read for the high byte is read beforefirst and then the Wire.read for the low byte. Even with the most extreme optimizations,That is pure luck as @EdgarBonet has corrected me below. In the orderc++ language there is in this situation still from left to rightno specified order of calling functions.
The bitwise 'OR' with the '|' could be a problem when there was no data and -1 was returned by Wire.read.

Yes and yes, although the compiler might give a warning, perhaps a type casting or conversion is needed.

Arduino has macros to make it easier: highByte, lowByte, and word.

You don't even have to split and combine bytes and words. The EEPROM.put and EEPROM.get can write two bytes if you want to. It's all documented in the reference of EEPROM.

There is a lot going on in this piece of code:

int16_t GyX;
GyX=Wire.read()<<8|Wire.read();

Shifting a single byte 8 bits to the left would be wrong. However, the Wire.read returns an integer and the GyX is an integer. Therefor the compiler uses integers for everything and shifting 8 bits to the left is valid.
The compiler will call the functions from left to right, so the high byte is read before the low byte. Even with the most extreme optimizations, the order is in this situation still from left to right.
The bitwise 'OR' with the '|' could be a problem when there was no data and -1 was returned by Wire.read.

@PetioPetrov, Arduino has macros to make it easier: highByte, lowByte, and word.

You don't have to split and combine bytes and words. The EEPROM.put and EEPROM.get can write and read two bytes if you want to. It's all documented in the reference of EEPROM.

There is a lot going on in this piece of code:

int16_t GyX;
GyX=Wire.read()<<8|Wire.read();

Shifting a single byte 8 bits to the left would be wrong. However, the Wire.read returns an integer and the GyX is an integer. Therefor the compiler uses integers for everything and shifting 8 bits to the left is valid.
At this moment with the current Arduino and avr-gcc compiler version, it will call the Wire.read for the high byte first and then the Wire.read for the low byte. That is pure luck as @EdgarBonet has corrected me below. In the c++ language there is no specified order of calling functions.
The bitwise 'OR' with the '|' could be a problem when there was no data and -1 was returned by Wire.read.

Source Link
Jot
  • 3.3k
  • 1
  • 14
  • 21

Yes and yes, although the compiler might give a warning, perhaps a type casting or conversion is needed.

Arduino has macros to make it easier: highByte, lowByte, and word.

You don't even have to split and combine bytes and words. The EEPROM.put and EEPROM.get can write two bytes if you want to. It's all documented in the reference of EEPROM.

There is a lot going on in this piece of code:

int16_t GyX;
GyX=Wire.read()<<8|Wire.read();

Shifting a single byte 8 bits to the left would be wrong. However, the Wire.read returns an integer and the GyX is an integer. Therefor the compiler uses integers for everything and shifting 8 bits to the left is valid.
The compiler will call the functions from left to right, so the high byte is read before the low byte. Even with the most extreme optimizations, the order is in this situation still from left to right.
The bitwise 'OR' with the '|' could be a problem when there was no data and -1 was returned by Wire.read.