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.)
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
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.
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.
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.
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.