# boolean

How much RAM does a boolean type take up. Is it only one bit? I’m trying to find a smaller data type than the byte declaration. So if I declare a 10x10 boolean array, I need only 100 bits of memory.

“boolean” in most C compilers will take up at least as much storage as “byte”
On some, it will be an “int”, since int’s are assumed to be the preferred size for computation.

You'll have to do some bit shifting. Hell, you probably want to write the assembly by hand if you want to do that.

On practically every computer in use today, from the ATTiny to the latest and greatest Intel chip, you'll find no register that is not a multiple of 8 bits. The Arduino language does not have any datatype like that; byte or char is the best you can do.

Even if you packed your bits in as efficiently as possible, you'd still need (104/8=) 13 bytes.

Just out of curiosity, what is the application that you need 100 booleans for?

Well, turns out I was wrong. How serendipitous that within a couple hours of posting that, I stumbled upon a code snipped demonstrating something like what you're looking for.

http://www.circuitsathome.com/mcu/lightweight-usb-host-part-6-hid#more-856

Googling brought up this:

Remember that no matter what number of bits you put in your struct, the compiler will ALWAYS align it on an 8-bit boundary (essentially filling in however many bits it needs to in order to get it to a multiple of 8 bits).

Bitfields don't necessarily solve the problem, and can make it more awkward, because you can't have arrays of bitfields, so you can't write,

``````typedef struct {
unsigned cell :1;  // I'd like a row of one hundred single bit booleans, please
} BOOL_ROW;
``````

The following isn't allowed either:

``````typedef struct {
unsigned cell :100;  // I'd like a row of one hundred single bit booleans, please
} BOOL_ROW;
``````

You can only have individually-named bit-fields (though of different widths), so forming and accessing a row of 100 can be tedious.

Some integer division, modulo arithmetic and shifting is probably the best you can hope for.

The way I would approach this is to balance size with flexibility and simplicity by using an array of 10 16-bit integers.

``````uint16_t bits;

// set bits[x][y]
bits[x] |= _BV(y);

// clear bits[x][y];
bits[x] &= ~_BV(y);

// test bits[x][y]
if ( bits[x] & _BV(y) )
;
``````

Nice balance, maniacbug.

Thank you for the posts guys very helpful.

I'm trying to digitize a 12 foot by 7 foot map with the UNO. I'm using a 2D array. I was planning on having a boolean array in stored in RAM which is why i want it so small.
The way it works, lets say you want the transition from each matrix element to represent 1 inch. In other words if you go from array*[j] to array[i+1][j] then you have just moved one inch north. So the resolution of the map is 1inch per matrix element. Thus, if you need a 12 foot by 7 foot map the minminum amount of memory would be:*
1. Matrix size row: 12 foot x 12inch/foot * 1element / 1 inch = 144 row elements
2. Matrix size column: 7foot x 12inch/foot * 1element/1inch = 84 col elements
3. Hence, we need a 144x84 matrix which contains a total of 12096 elements. These elements are a digital representation of the map.
4. If those elements are bytes then the amount of memory needed is 12KB approx which can only be stored in flash memory, but if they where bits, 0 or 1 then the memory needed is 12096 bits x (1 byte/8 bits) 1512bytes. You can see the dramatic reduce in memory.
The other way to do this is to make the resolution worst, say 2 inch /element.

This sounds like a good project for an SD card. You could store your results there, and then its easy to get them back to your pc.

@AWOL, thanks!

What information do you need to store for each cell in the matrix representing the map? This will tell you how much memory you need.

There are two possibilities right now:

1. Just a bit, 0 or 1. 0 = not occupied, 1 = occupied

2. Byte but this would have to be in Flash Memory. It would be a number between 0-100 a “priority” or probability.

How many pins do you need to interface the SD card. How difficult is it, becuase I'm not seasoned in serial comm.