Just curious

I know that when I declare a boolean variable, it's stored as just one bit.
What happens if I declare several boolean variables (say 8)?
Does the compiler 'assemble' all the bits into one byte, much like a flag byte, or are the various bits stored in different places?

If they're stored in one byte, can that byte be accessed and manipulated?
(Why anyone would want to do that is beyond me but, as the subject line says, I'm just curious.)

A bool is one byte, not a bit.
A bool is best not messed with ( apart from true/false ), just use a byte.

Here is a link to a class I made that uses bits for boolean values. BitBool Class ( boolean array ), upto 16000 elements on UNO. - Libraries - Arduino Forum

Boolean types take a whole byte. "boolean" is a typedef to "uint8_t" which in turn is "unsigned char".

Using bit manipulation you can store 8 "booleans" in a byte, 16 in an int, or 32 in a long... And much more, using an array of longs and functions to write/read the state of a bit to/from this array.

Here is some thing I wrote few days ago, that may interest you, mr curious :slight_smile:

http://codepad.org/bQkrAFyG

@gardner & @pYro_65

Thanks for your replies. I seem to have misunderstood how a boolean is stored.

@guix,
Yep. I get that, (I've written assembly code* and know about bit manipulation) but was wondering how the compiler handled them.
*Z80 assembler, many years ago.


It does seem wasteful of space for the compiler to store 8 single bits as 8 bytes.

Henry_Best:
It does seem wasteful of space for the compiler to store 8 single bits as 8 bytes.

C++ provides no method of addressing anything smaller than a byte, just like you cannot get the address of bit field members.

On the contrary:

struct bitField
{
  boolean bitOne: 1;
  boolean bitTwo: 1;
  boolean bitThree: 1;
  boolean bitFour: 1;
};

byte test;
struct bitField glorp = {false, true, true, false};

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  test = *(byte *)(&glorp);
  Serial.println(test);
  
  for(;;);
}

Output:

6

That does not change anything.

you cannot get the address of bit field members.

If they're stored in one byte, can that byte be accessed and manipulated?

I manipulate single bits in a byte all the time.
Simplest might be a slave select bit, using direct port manipulation vs digitalWrites:

PORTB = PORTB & B11111110; // clear bit 0, leave the rest alone

PORTB = PORTB | B00000001; // set bit 0, leave the rest alone

In case anyone following this thread has a need to store packed bools...
http://forum.arduino.cc/index.php?topic=128407.msg965724#msg965724

(odd you didn't mention it pYro_65)

Nice, good to see some people have a use for it. I did link it in reply 2 though :slight_smile:

pYro_65:
I did link it in reply 2 though :slight_smile:

That's embarrassing.

In my defense, while I was reading your post, our dog started chasing a rat around the backyard. I must not have made it to the link before the ruckus ensued.

It does seem wasteful of space for the compiler to store 8 single bits as 8 bytes.

It is, and in the "old days" it was very common to pack single-bit flags and various other < 8-bit vars into bytes. That was when your whole application only had maybe 50 bytes or so of RAM.

These days we tend to have enough memory so it's less likely you'll need to do it, but still can be necessary if things get really tight.


Rob

It does seem wasteful of space for the compiler to store 8 single bits as 8 bytes

It's all tied-in to the C philosophy of pointers - imagine the situation where you had something like

void myFunction (bool* flagPointer...) 
{
/// some function

Since you cannot represent the address of a single bit in a common form, you cannot form a pointer to it.

Graynomad:

It does seem wasteful of space for the compiler to store 8 single bits as 8 bytes.

It is, and in the "old days" it was very common to pack single-bit flags and various other < 8-bit vars into bytes. That was when your whole application only had maybe 50 bytes or so of RAM.

These days we tend to have enough memory so it's less likely you'll need to do it, but still can be necessary if things get really tight.


Rob

What do you mean "the old days"?! There are modern microcontrollers with less RAM than that, for when you only need to do a "small" job and something like a '328 os OTT.

Take the PIC10F200 for example... 16 bytes of SRAM. You have to be as careful with your memory use there as you did "in the old days" - if not more so.

:slight_smile:
I guess some architectures never left the old days.

16 bytes of SRAM

That's not memory, that's a few registers :slight_smile:

Take the PIC10F200 for example

I'd rather not. Why are you trying to get rid of them?


Rob

Graynomad:
:slight_smile:
I guess some architectures never left the old days.

16 bytes of SRAM

That's not memory, that's a few registers :slight_smile:


Rob

Sometimes a few registers is all you need. Why pay for 2K of RAM when all you need is 9 bytes?

Graynomad:

Take the PIC10F200 for example

I'd rather not. Why are you trying to get rid of them?

The 10F20x are dedicated for disposable applications..

pito:

Graynomad:

Take the PIC10F200 for example

I'd rather not. Why are you trying to get rid of them?

The 10F20x are dedicated for disposable applications..

I used one once to design an electronic candle. All it needed was red, green, blue, duty cycle counter, and a couple of bytes for generating a pseudo-random number. The chip itself was a 6-pin SC23, so was one of the smallest components on the board. The whole thing was about the size of (and ran off) a CR2032 battery and sat inside a tea-light. At a bulk price of $0.30 you can afford to have them disposable.

At a bulk price of $0.30 you can afford to have them disposable

That is single quantity, imagine the price for 1mil pieces - you can build a blinking/fading/beeping cigarettes, for example.. :slight_smile: