Question: I have an array with 16 elements. I would like to evaluate these to a boolean 0 or 1 and then store this in 2 bytes so i can write to a binary file. How do I do this?

Answer: 
Declare an array `result` with two bytes, then you loop through the source array:

```
for (int i = 0; i < 16; i++) {
  // calclurate index in result array
  int index = i >> 3;
  // shift value in result
  result[index] <<= 1;
  // check array value
  if (theArray[i]) {
    // true, so set lowest bit in result byte
    result[index]++;
  }
}

```
Something like this.

```
int values[16];
int bits = 0;
for (int ii = 0; ii < 16; ++ii)
{
    bits |= (!!values[ii]) << ii;
}

unsigned short output = (unsigned short)bits;

```

the expression (!!values[ii]) forces the value to be 0 or 1, if you know for sure that the values array already contains either a 0 or a 1 and nothing else, you can leave of the !!

You could also do this if you don't like the !! syntax. 

```
int values[16];
int bits = 0;
for (int ii = 0; ii < 16; ++ii)
{
    bits |= (values[ii] != 0) << ii;
}

unsigned short output = (unsigned short)bits;

```