tags:

views:

184

answers:

6

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?

+1  A: 

You have to use bitwise operators.

Here's an example:

int firstBit = 0x1;
int secondBit = 0x2;
int thirdBit = 0x4;
int fourthBit = 0x8;

int x = firstBit | fourthBit; /*both the 1st and 4th bit are set */
int isFirstBitSet = x & firstBit; /* Check if at least the first bit is set */
Brian R. Bondy
+4  A: 

Something like this you mean?

unsigned short binary = 0, i;
for ( i = 0; i < 16; ++i )
  if ( array[i] )
    binary |= 1 << i; 

// the i-th bit of binary is 1 if array[i] is true and 0 otherwise.
IVlad
exactly like that, thanks
Aran Mulholland
+2  A: 
int values[16];
int i;
unsigned short word = 0;
unsigned short bit = 1;

for (i = 0; i < 16; i++)
{
    if (values[i])
    {
        word |= bit;
    }

    bit <<= 1;
}
James
+1  A: 

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]++;
  }
}
Guffa
Why the downvote? If you don't say what it is that you don't like, it's rather pointless.
Guffa
I didn't vote in any way, but I'm guessing it's because you use an array and you don't even mention its type. I'm guessing it's a char array, but the OP might not know that. Either way, you can do it without an array. Is there a reason you chose to use an array?
IVlad
@IVlad: The OP specifically asked for two bytes, the logical way to store those is in an array. The data type can be anything capable of handling a byte, but a char would be an obvious choise.
Guffa
Well, a short can also work, can't it? Anyway, +1 from me, I guess a char array does fit better :).
IVlad
The OP specified an array of 16 elements as input/ He did not specify what type, but it does not actually matter, so long as whatever it is can be evaluated to zero or non-zero and therefore converted into a boolean.
Clifford
@Clifford: Yes, what is your point?
Guffa
@Guffa: The point was in response to IVlad's comment. IVlad suggested it was down-voted because the type was not mentioned; I was merely clarifying that the type is irrelevant and that the code is correct in the context of the question. IVlad said that the OP might not know what the type is, but in fact it is the OP that is defining it.
Clifford
A: 

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;
John Knoeller
What's the point of using `!!`? A simple `if` statement makes this much easier to read. And your `for` loop doesn't compile, by the way.
IVlad
an if statement adds a branch in the inner loop.
John Knoeller
Sweet. suggest a more optimal soluton and get downvoted for it! Thanks LVlad!
John Knoeller
Really, more optimal? It's just an array of 16 elements, I doubt there's ANY platform where you could notice a difference. It's just needlessly obfuscating the code here.
IVlad
Really a lot more optimal. Test it yourself if you don't believe me. Little functions like this tend to get used only once. OR they get used all of the time and suddenly this branch in an inner loop starts to matter.
John Knoeller
+2  A: 

This solution avoid the use of the if inside the loop:

unsigned short binary = 0, i;
for ( i = 0; i < 16; ++i )
  binary |= (array[i] != 0) << i;
Maurizio Reginelli
It means that the loop shifts zeroes left and or's the result into 'binary', whereas IVlad's solution only processes set bits. It isn't clear on a majorly pipelined architecture where the benefit is, but nominally doing fewer operations for an array with many zeroes is a benefit.
Jonathan Leffler