Skip to main content
+ cast to unsigned and reference.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

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

This is a bug. It reads two bytes from the I2C bus and builds a 16-bit number from them. One of the bytes (we don't know which one) will be the most significant byte of the result. The other byte will be the least significant byte.

This instruction is likely to work just fine on some particular version of some particular compiler with some particular compiler options. Presumably it did work for the author. But you should not expect it to work consistently across compilers, or even across different versions of the same compiler.

The proper way to do this is to perform the two reads in different instructions:

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

assuming the bytes come most-significant first.

Edit: added (uint16_t) cast. C.f. this answer.

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

This is a bug. It reads two bytes from the I2C bus and builds a 16-bit number from them. One of the bytes (we don't know which one) will be the most significant byte of the result. The other byte will be the least significant byte.

This instruction is likely to work just fine on some particular version of some particular compiler with some particular compiler options. Presumably it did work for the author. But you should not expect it to work consistently across compilers, or even across different versions of the same compiler.

The proper way to do this is to perform the two reads in different instructions:

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

assuming the bytes come most-significant first.

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

This is a bug. It reads two bytes from the I2C bus and builds a 16-bit number from them. One of the bytes (we don't know which one) will be the most significant byte of the result. The other byte will be the least significant byte.

This instruction is likely to work just fine on some particular version of some particular compiler with some particular compiler options. Presumably it did work for the author. But you should not expect it to work consistently across compilers, or even across different versions of the same compiler.

The proper way to do this is to perform the two reads in different instructions:

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

assuming the bytes come most-significant first.

Edit: added (uint16_t) cast. C.f. this answer.

Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81

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

This is a bug. It reads two bytes from the I2C bus and builds a 16-bit number from them. One of the bytes (we don't know which one) will be the most significant byte of the result. The other byte will be the least significant byte.

This instruction is likely to work just fine on some particular version of some particular compiler with some particular compiler options. Presumably it did work for the author. But you should not expect it to work consistently across compilers, or even across different versions of the same compiler.

The proper way to do this is to perform the two reads in different instructions:

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

assuming the bytes come most-significant first.