Go Down

Topic: Sizeof Bools (Read 909 times) previous topic - next topic


Jun 08, 2008, 03:11 am Last Edit: Jun 08, 2008, 08:51 am by scottyob Reason: 1

I was reading up how many operating systems can not deal with accessing single bits, and in fact, deal with single bytes.  This just means that a bool data type will take up 8 bits in memory instead of 1.  I was wondering, does the Arduino have the same downfall? Is there a way we could modify it to be more memory efficient (just guessing, because we don't really have an OS to work with here)




After trying to sizeof a bool, it appears that booleans are stored as a byte in the Arduino. you can't really do anything about it. If you need to use large arrays of booleans values, you could store them as arrays of byte.. But as with any optimization, you should use tools to check what needs to be optimized before optimizing.
... could use some sleep


If you want to be more memory efficient, declare a flag byte and use it as eight independent bools (each bit of the byte being one of your bools).  I believe this is what tehboii was suggesting.  The AVR won't internally do this sort of thing for you.  There are some tricks you can do with structs that will let you use struct members to access single bits of a byte.

- Ben


Jun 08, 2008, 07:37 am Last Edit: Jun 08, 2008, 07:38 am by tehboii Reason: 1
Thank you, after reading back my own answer, i realize I couldn't have been less clear.

I'm not sure, anyway (but it needs to be tested) that this will be efficient for a single byte, since it requires additional code which will also take a few space in memory. But with big arrays of booleans (like a Game of Life's game field matrix) it would be an extremely room-saving thing to do.

"code first, optimize later" ^_^
... could use some sleep


"code first, optimize later" ^_^

Yeah, so far I've never had a program with so many bools that it would have been worth condensing them into bytes.  I guess I might do it if I were making a huge game of life (where I only need to record if each cell is occupied or vacant).

- Ben


If you have a lot of booleans and are concerned about running out of memory you can use the C bitfield construct. This allows several items to be packed into the space of single variable. The syntax used is in the form of a structure, here is an example of eight Booleans packed into a single byte:

struct  {
     byte isState1           :1 ;
     byte isState2           :1 ;
     byte isState3           :1 ;
     byte isState4           :1 ;
     byte isState5           :1 ;
     byte isState6           :1 ;  
     byte isState7           :1 ;
     byte isState8           :1 ;        
    myStates ;

 if(myStates.isState1 && myStates.isState8)  // turn the led on if State1 and State8 are true

google bitfields to read more if this is something you want to use to squeeze your sketch to fit into RAM.


That is very good to know.  Thanks guys :)


google bitfields to read more if this is something you want to use to squeeze your sketch to fit into RAM.

I can never remember the syntax for that, but maybe one of these days it will stick (it's not even complicated!).  Thanks, mem.

- Ben

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131