bit/byte help needed

Hello, So this is my first arduino project for the most part so sorry if it isnt terribly descriptive. I am trying to use a sparkfun 8x8 RG led matrix with my arduino. The leds are referenced at zero, so 0-63 for the positions. I have it reading a byte from MAX/MSP and lighting said LEDS. The on/off/color value is set on the arduino right now, so it turns on whichever number LED it receives. So there are two unused bits in the byte that i wanted to use for off, red, green, or orange. I do not know how to do this though. Currently, if i set those bits high it changes the 0-63 led position number. How do i use 2 of the bits for color and 6 for position, without them affecting one another? I tried using two separate bytes but couldnt figure out how to read them separately so that say, byte1 would be x, byte 2 would be y... If that makes any sense to you, id really appreciate any help...

define RGO_MASK 0xC0

define POS_MASK 0x3F

define RED 0x40

define GREEN 0x80

define ORANGE 0xC0

... ...

byte red_green_orange = (packed_byte & RGO_MASK); byte led_pos = packed_byte & POS_MASK;

I realized i dont know how to implement this, tried a few things, didnt work. This is my code for original post… heh sorry, VERY new.

#define CHIPSELECT 10//ss
#define SPICLOCK  13//sck
#define DATAOUT 11//MOSI / DI
#define DATAIN 12//MISO / DO

int data[64] =
{
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};

char spi_transfer(volatile char data)
{
SPDR = data;                    // Start the transmission
while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
{
};
}


void setup()
{
Serial.begin(9600);
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device

SPCR = B01010001;             //SPI Registers
SPSR = SPSR & B11111110;      //make sure the speed is 125KHz
/*
SPCR bits:
 7: SPIEE - enables SPI interrupt when high
 6: SPE - enable SPI bus when high
 5: DORD - LSB first when high, MSB first when low
 4: MSTR - arduino is in master mode when high, slave when low
 3: CPOL - data clock idle when high if 1, idle when low if 0
 2: CPHA - data on falling edge of clock when high, rising edge when low
 1: SPR1 - set speed of SPI bus
 0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
 */

clr=SPSR;
clr=SPDR;
delay(10);
}

int x = 0;

void matrixstate()
{
  data[x] = 3; // 3 = orange
}

void loop()            
{
  if (Serial.available() > 0 ) {
            // read the incoming byte:
            x = Serial.read();
            // Serial.print("I received: ");
            // Serial.println(x, DEC);
  }
 
 delay(100);
 matrixstate();
 int index = 0;                 
 digitalWrite(CHIPSELECT,LOW); // enable the ChipSelect on the backpack
 delayMicroseconds(500);
 for (int i=0;i<8;i++) for (int j=0;j<8;j++)
 {
   spi_transfer(data[index]);
     index++;                  
 }
 digitalWrite(CHIPSELECT,HIGH); // disable the ChipSelect on the backpack
 delayMicroseconds(500);

}

What do you expect this to do? What do you want this to do?

int x = 0;

void matrixstate()
{
  data[x] = 3; // 3 = orange
}

Better:

void matrixstate(int x)
{
    data[x] = 3; // 3 = orange
}
...
matrixstate (x);

Then you have to check the value of "x" you're receiving, making sure it is 0..63.

I want it to update the array number that X is with 3. Id like to replace the 3 with a variable determined by 2 bits of x (bit 6 and 7) from Serial.read();. Without those two bits affecting the value of x. So bits 0-5 for a 0-63 x position, and bits 6-7 for 0-3 for colors (say y). I dont know how to use different bits in a byte for different things, so right now the whole byte is being used as x for the led position. So 6 and 7 must be low right now for any LEDS to light.

Thanks much Royk.

There was a typo in my earlier post with this same information. It would no longer let me modify the post so I deleted it and put the corrected version here.

unsigned char x;
bits numbered from 0 to 7 with bit 0 the least significant

To read only bits 0-5

y = x & 0x3f // 0x3f = 00011111 binary (bits 0-5 set to 1)

to read the values of bits 6 & 7

y = (x & 0xc0) >> 6 //0xc0 = 11000000 binary (bits 6 and 7 set to 1)

The idea is to use a ‘mask’ with the bits you want to look at set to 1 and do a logical ‘and’ (&) with the byte you want to check. That will set y to x with all of the bits that aren’t ‘1’ in the mask to zero and the bits that are 1 left as they are.

for example if x is 10111001
and mask is 00011111

then y=x & mask is 00011001

The >> 6 in the second example moves bits 6 and 7 over to position 0 & 1 so you can easily check them like for example:

if(((x & 0xc0)>>6) == 2){…}

Other handy bit manipulations:
To test bit n in byte x

if(x & ( 1 << n))
{
here if the bit is 1
}
else
{
here if the bit is 0
}

Other manipulations

to set bit n to 1

x |= (1 << n)

to clear bit n (set it to 0)
x &= ~(1 << n))

…and to flip a bit:

x ^= (1 << n)