Home > Mobile >  How to convert two dimensional byte array into single byte (arduino)
How to convert two dimensional byte array into single byte (arduino)

Time:05-07

I have this array:

bool data[] = {{1,1,1,1},{0,0,0,0}};

And I need to convert it into bool data type like this:

byte data = 11110000;

Any ideas?

CodePudding user response:

You can try something like this:

#define COLUMNS 2
#define ROWS 4
#define SIZE (COLUMNS * ROWS)


bool data[COLUMNS][ROWS] = {{1, 1, 1, 1}, {0, 0, 0, 0}};

byte result = 0;
int shift = SIZE;

for(int i = 0; i < COLUMNS; i  ) {
    for (int j = 0; j < ROWS; j  ) {
        result |= data[i][j] << --shift;
    }
}
  • Related