Go Down

Topic: Just curious (Read 1 time) previous topic - next topic

Henry_Best

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

pYro_65

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. http://forum.arduino.cc/index.php?topic=128407.msg965724#msg965724

gardner

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

guix

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

http://codepad.org/bQkrAFyG

Henry_Best

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

pYro_65


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.

TanHadron

On the contrary:

Code: [Select]

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:
Code: [Select]
6

pYro_65

That does not change anything.

Quote
you cannot get the address of bit field members.



CrossRoads

Quote
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

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Coding Badly


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)

pYro_65

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

Coding Badly

I did link it in reply 2 though :)


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.

Graynomad

Quote
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
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

Quote
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
Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

majenko


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

Go Up