bitRead function for word

Hi,
i have a problem with understanding the zero shift in byte information.
If I have "byte = B11111111" than "bitRead(byte, 0)" is 1.
If I have "byte = B11111110" than "bitRead(byte, 0)" is bug.

uint16_t data_r;
uint16_t data_old;
uint8_t val[15];
uint8_t val_old[15];

...

Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  if (Wire.available()) {
    uint8_t lo = Wire.read();
    uint8_t hi = Wire.read();
    data_r = (word(hi, lo));
    if (data_r != data_old) {
      for (uint8_t i = 0; i < 16; i++) {
        val[i] = (bitRead(data_r, i ));
        if (val[i] != val_old[i]) {
          Serial.print("button: ");
          Serial.println(i);
          Serial.print("value:  ");
          Serial.println(val[i]);
          Serial.print("data:   ");
          Serial.println(data_r, BIN);
          val_old[i] = val[i];
        }
      }
      data_old = data_r;
    }
    delay(100);
  }

Is it possible to work with zero as a number without omitting a position?

If I have "byte = B11111111" than "bitRead(byte, 0)" is 1.
If I have "byte = B11111110" than "bitRead(byte, 0)" is bug.

Can you please explain more fully what the problem is ?

The problem is 0 and 15, the result of the code for val[0] and val[15] at byte = B11111110 (byte = B01111111) is wrong. I think the code omit zero as a number if it is at the beginning or end of the value byte.

val[15] doesn't exist, so you should not try to read or write to it.

The problem is 0 and 15, the result of the code for val[0] and val[15] at byte = B11111110 (byte = B01111111) is wrong. I think the code omit zero as a number if it is at the beginning or end of the value byte.

bitRead() is defined in Arduino.h

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)

What is the issue with this code which you have posted?

if (Wire.available()) {
    uint8_t lo = Wire.read();
    uint8_t hi = Wire.read();
    data_r = (word(hi, lo));
    if (data_r != data_old) {
      for (uint8_t i = 0; i < 16; i++) {
        val[i] = (bitRead(data_r, i ));
        if (val[i] != val_old[i]) {
          Serial.print("button: ");
          Serial.println(i);
          Serial.print("value:  ");
          Serial.println(val[i]);
          Serial.print("data:   ");
          Serial.println(data_r, BIN);
          val_old[i] = val[i];
        }
      }

val[0] to val[15] is 16 elements. If you want to access element 15 in val and val_old, you need to change these lines:

uint8_t val[15];
uint8_t val_old[15];

to this:

uint8_t val[16];
uint8_t val_old[16];

Thank christop! The error is between the keyboard and the chair... I incorrectly setup the arrays :frowning: