split a byte into 8 variables

Hi

how do I split a byte into 8 variables, 1 for each bit in the byte?

for example, serially i receive a byte coming in like 11010111

and i want to split that into 8 variables so that...

int variable1 = 1
variable2 = 1
variable3 = 1
variable4 = 0
varibale5 = 1
variable6 = 0
variable7 = 1
variable8 = 1

this seems like a simple task but for some reason I'm having trouble in my researching with finding a way to do it. I need a simple example code and what ever you want to teach me about it is cool to. Thanks.

-Epic

Would you prefer variable to be an array of int?

an array of ints would be great! :sunglasses:

This (untested code) should do it...

void CrackByte( byte b, int variable[8] )
{
  byte i;
  
  for ( i=0; i < 8; ++i )
  {
    variable[i] = b & 1;
    b = b >> 1;
  }
}

void setup( void )
{
  byte b = 0xD7;
  int variable[8];
  
  CrackByte( b, variable );
}

void loop( void )
{
}

thanks for the example and quick reply(s).

-Epic

If you're uncomfortable with C bit manipulation, bitRead may be your answer:
http://www.arduino.cc/en/Reference/BitRead

Best book: Practical C Programming (or Practical C++ Programming) from O'Reilly. Great refs for coding.

Best online I could find: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871

To help understand the CrackByte code example consider this:

byte data = b11010111;
byte mask = b0000001;
var1 = data & mask;  //ref the AND logic table to see that this is always getting the value at the right most bit.

data >> 1;  //01101011
var2 = data & mask;
data >> 1;  //00110101
var3 = data & mask;

etc...

Now when flash space is a problem, you have reduced your 16byte int array to 2 bytes.

BitMasks are great for tracking stuff that is usually on/off state flags, etc.