Pages: [1]   Go Down
Author Topic: Sizeof Bools  (Read 675 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

G'Day

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)

Thanks,

Scotty
« Last Edit: June 08, 2008, 01:51:06 am by scottyob » Logged

Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

... could use some sleep

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Paris FR
Offline Offline
Full Member
***
Karma: 0
Posts: 155
cute little geek...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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" smiley-kitty
« Last Edit: June 08, 2008, 12:38:32 am by tehboii » Logged

... could use some sleep

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"code first, optimize later" smiley-kitty
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
Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ;

usage:
  if(myStates.isState1 && myStates.isState8)  // turn the led on if State1 and State8 are true
     digitalWrite(led_pin,HIGH);

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is very good to know.  Thanks guys smiley
Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: