Confused with byte [SOLVED]

I have the following question, mostly due to my lack of knowledge on the subject....

I am working with an mcp23017 and I want to generate a byte in order to get the output pins HIGH or LOW.

Have an array of 0 and 1 that I want to convert to the final byte, is that possible?

eg.

byte myByte[8] = {0,1,1,1,0,0,0,1};
//and I want something like this as a result
uint8_t final = B01110001;

Different approaches are possible but try the bit write function. You can lay out one write after another or automate by setting up a for() loop.

uint8_t final = B01110001;

or
uint8_t final = bit(7) | bit(6) | bit(5) | bit(1)

See also: bit versus _BV

test it:

void setup() {
  Serial.begin(115200);
  byte myByte[8] = {0, 1, 1, 1, 0, 0, 0, 1};
  //and I want something like this as a result
  //uint8_t final = B01110001;
  uint8_t final = 0;
  for (size_t i = 0; i < sizeof(myByte); i++)
  {
    if (myByte[i]) final |= bit(7- i + 1);
  }
  Serial.println(final, BIN);
}

void loop() {
}

You draw eight connected boxes, label each box from right to left with the numbers 0 to 7. Put a double line dividing boxes 3&4.
Write down the values of 0 or 1 depending on what bits you want to set or clear.

Then turn each group of four boxes into a hexadecimal digit, use that digit to set your variable
Variable = 0x2C
For example.

As you get good most of this you can do in your head, but start like this.

Why not save the values as bits in a single byte in the first place ?
Where do the values come from ?

@dougp
The bitWrite() works like a charm! It was so easy and took me so long to figure, thanks for the help!

@noiasca
Will do some more reading on your approach, since mine looks a little crude with simple for loops and ifs. Thanks for the info!

@Grumpy_Mike
That is a fine explanation to grasp, thanks a lot!

@UKHeliBob
The values are coming from a json file that I read and need to set up different pins on the final end of the MCP. That would be the easiest way to do it but the json needs to be well formated for easy reading and updating, so in the long run it would create a mess...

Thanks all for your quick responses!

dealing with a I/O register values depends on what the bits mean. groups of bits may control something or there may be individual bits.

individual bits may be easier to represent with symbol names

#define LED_1   0x01
#define LED_2   0x02
#define LED_3   0x04
#define LED_4   0x08

#define LED_ALL (LED_1 | LED_2 | LED_3 | LED_4)


    value1 |=  LED_3;        // turn on LED_3
    value1 &= ~LED_1;        // turn off LED_1

    ioWr (device, register1, value1);

gcjr:
dealing with a I/O register values depends on what the bits mean. groups of bits may control something or there may be individual bits.

individual bits may be easier to represent with symbol names

#define LED_1   0x01

#define LED_2   0x02
#define LED_3   0x04
#define LED_4   0x08

#define LED_ALL (LED_1 | LED_2 | LED_3 | LED_4)

value1 |=  LED_3;        // turn on LED_3
   value1 &= ~LED_1;        // turn off LED_1

ioWr (device, register1, value1);

I thought about that but then I will have to define 8^8 possible combinations...

The values are coming from a json file

Does your sketch put them in the array from individual values contained in the JSON file or do they get delivered as an array of bytes ?

Seeing an example of the JSON file and how you put the entries in the array would be interesting

panos_gkrigkas:
I thought about that but then I will have to define 8^8 possible combinations...

i suggested conventional approaches for dealing with register bits. of course there are more generic approaches

it would help to know what your application is