More RAM with a boolean array? Arduino Environment

I have a code that I have in an Arduino environment that requires x (in increments of 8) Boolean values ​​that manipulate at runtime for some shift register code. Therefore, I am currently using such a logical array:

 #define number_of_shiftRegisters 220 //num of 8 bit shift registers

 #define numOfRegisterPins number_of_shiftRegisters * 8 //number of booleans needed

 boolean registers[numOfRegisterPins]; //boolean array

But I had about 200 (1600 Boolean) memory, and I did not know why, until I saw this, although the logical values ​​are 1 bit, they are stored in 8 bits of data.

As I said, the number of bools needed always increases in increments of 8, so I don’t know if this can work in my interests.

Is there a more efficient way to store more than 1000 boolean values ​​and can still reference them by index?

Or ... At least a more efficient memory that won't cost significantly more CPU time to install and repeat through?

I thought of an array char, and then a bit masked each char to access individual bits. But I did not know if there was an easier way, or if it would require significantly more processor time.

+5
source share
2 answers

Yes, you can easily use disguise to get around this problem.

Each byte (without a char sign) will contain 8 logical values, to get the i-th, you can just use it values & (1 << i)in the case of tests, and it will work if the correct bit is set, then the result will be! = To 0.

, / : values | (1 << i) ( unset AND 0).

:

struct Bools
{
  int b1 : 1;
  int b2 : 1;
  int b3 : 1;
  ... // up to b8
}

8 booleans, .

+6

, .

unsigned char , :

  • ,
  • , ,
  • , .
  • ,
  • /

:

  • ,
  • /

, , . , - , . .

, , , , . optmization, , , . .

, , Arduino, , , unsigned int unsigned long, couterproductive.

+3

All Articles