Bitwise operation examples

Hi--

I want to store a few bytes in EEPROM that, for example, control which LED's were on when it powered down. The logical way to do this is by breaking each EEPROM byte into 8 bits, and using those bits to store the digital (HIGH/LOW) values. The section I need is bitwise functions, but I've read through the tutorial and don't have a firm grasp of the functions required... Does anybody have any examples?

Specifically, I want to, loop through an array of 8 booleans and store a single byte in EEPROM representing those values, and then be able to read that byte back and set up the array the way it was, but being able to populate a single boolean value with, say, the third bit of a particular byte would be useful as a start...

Thanks,

-Matt-

Here's an example:

byte
packbyte(byte b[],int n)
{
        int idx;
        byte onebit;
        byte val;

        val = 0;
        idx = 0;
        while (n > 0)
          {
          /* turn a boolean into a single bit, true = 1, false = 0 */
          if (b[idx])
            onebit = 1;
          else
            onebit = 0;
          /* shift the current value left one bit, then OR in the new bit */
          val = (val << 1) | onebit;
          idx = idx + 1;
          n = n - 1;
          }
        return(val);
}


void
unpackbyte(byte  z,int n,byte b[])
{
        while (n > 0)
          {
          /* extract out the least significant bit from 'z', */
          /* and put it into the array, working from the end of */
          /* the array to the beginning. */
          b[n - 1] = z & 1;
          /* shift the bits in 'z' to the right by 1 bit */
          z = z >> 1;
          n = n - 1;
          }
}

void
whatever()
{
        byte bool_array[8];
        byte packedbyte;

/* do something that fills in bool_array */

        packedbyte = packbyte(bool_array,8);

/* do something that sets packedbyte */

        unpackbyte(packedbyte,8,bool_array);
}

The only tricky part is being consistent about which bit goes where in the array. In the above example, the most significant bit from the assembled byte goes in the first ([0]) element of the array.

Regards,

-Mike

Also, please note that my example should work for 1 to 8 bits, but won't work with more than 8 bits.

-Mike

?being able to populate a single boolean value with, say, the third bit of a particular byte would be useful as a start...

bitWrite(x, bitPosition, value) sets the given value (as 0 or 1) of the bit at the given bitPosition of variable x . Note that bitPosition 0 is the least significant (rightmost) bit.

bitRead(x, bitPosition) returns the value (as 0 or 1) of the bit at given bitPosition of variable x

so:
bitWrite(x, 2, value); //sets the third bit (from the right) in variable x to the given boolen value. x can be a byte or int.

bitRead(x, bitPosition) returns the value (as 0 or 1) of the bit at given bitPosition of variable x

for example:
boolean value = bitRead(x, 2); //gets the boolean value in the third bit from variable x

Is there some reason that you're short of EEPROM space that you need to do this conversion? Seems like a lot of effort to go through to save a couple of bytes of EEPROM space. Just store the whole array?

[edit]I'm not trying to put down the value of efficiency in processing or data storage. Nor am I ignoring the finite write cycles of EEPROM. I'm just saying that sometimes we need to look beyond the trees to see the forest.[/edit]

Here's another thought. In your main logic, instead of using an array, use bitWrite() and bitRead() directly.

Replace all occurremces of
ledArray*[/b] (or whatever)*
with
_ bitWrite(ledByte, i, ...)
or
bitRead(ledByte, i)_
Your main logic, loops, etc. will otherwise remain the same, you will remove the need for a conversion when storing and retrieving from EEPROM, you will have minimum RAM use and minimum EEPROM use, all rolled into one.
If you have more than 8 elements in your boolean array now, you will need a little more creativity with bitWrite() and bitRead(). Without seeing your original code, it's a little hard to come up with an exact replacement, but I hope you get the idea.