Pages: [1] 2   Go Down
Author Topic: Just curious  (Read 1042 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cool?
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.)
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 911
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

http://codepad.org/bQkrAFyG
Logged

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
God Member
*****
Karma: 25
Posts: 529
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the contrary:

Code:
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:
6
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That does not change anything.

Quote
you cannot get the address of bit field members.

Logged


Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I did link it in reply 2 though smiley

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8620
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26627
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

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

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1] 2   Go Up
Jump to: