Bool size 1bite 1 byte or 2 bytes

I don't understand.

Some say that the bool or boolean size is one bit.

The Arduino reference says: Each bool variable occupies one byte of memory.

BUT

I ran a program to read the addresses of two booleans.

and from the addresses, I see that each one uses two bytes.

Can you help me understand which is correct?

Thank you

Antonio

it is no place in flash smaller as 8bit, so yes, boolean take 1 full byte in arduino platform. of course you able to pack 8 your bool variable in 1 byte and spare 7 bytes of flash

Thank you Kolaha.
I still don't understand why the address of one bool and another bull.
jumps by two numbers in the memory. So each one is occupying 2 bytes in the memory.
See next attachments
sketch_apr28a.ino (616 Bytes)


2 bytes in the memory

What Arduino board are you using?

What is the output, for your board, from this code?

bool someBool = false;

void setup()
{
   Serial.begin(115200);
   Serial.print("the size of a bool on this board is ");
   Serial.print(sizeof(someBool));
   Serial.println(" bytes");
}

void loop()
{

}

My Uno yields:

the size of a bool on this board is 1 bytes

Looks like you are printing the address of a pointer to a bool, not the address of the bool itself. On an UNO a pointer is 16 bits.

bool SmplBl_1 = true;
bool *pSmplBl_1 = &SmplBl_1;

bool SmplBl_2 = true;
bool *pSmplBl_2 = &SmplBl_2;

boolean Bln_1 = false;
boolean *pBln_1 = &Bln_1;

boolean Bln_2 = false;
boolean *pBln_2 = &Bln_2;


void setup() {
  Serial.begin(115200);
}

void loop() {
  int AddressOfpSmplBl_1 = & pSmplBl_1;
  Serial.println(AddressOfpSmplBl_1);
  int AddressOfpSmplBl_2 = & pSmplBl_2;
  Serial.println(AddressOfpSmplBl_2);

  int AddressOfpBln_1 = & pBln_1;
  Serial.println(AddressOfpBln_1);
  
  int AddressOfpBln_2 = &pBln_2;
  Serial.println(AddressOfpBln_2);
  
  while (!Serial.available()) {
  }
  Serial.read();
}

Thank you, David;
You are right But if the next bool is 2 bytes apart. how can I use the adjacent byte?

Hello tonydg

Consider this declaration and initialization of an "array" of bits.

enum MyBits {Bit_0,Bit_1,Bit_2,Bit_3,Bit_4,Bit_5,Bit_6,Bit_7};
uint8_t myBool {0b00000000}; // start setup of bits set
void  setup()
{
}
void loop()
{
}

Simply process these bits by using

grafik

Have a nice day and enjoy coding in C++.

It is not two bytes apart. You were printing the address of a pointer, which occupies two bytes, not the address of a bool, which occupies one. Try the following code, note that I put the bool variables in a struct, otherwise the compiler can scatter them throughout memory at its convenience.

struct {
bool SmplBl_1 = true;
bool SmplBl_2 = true;
boolean Bln_1 = false;
boolean Bln_2 = false;
} bools;

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println((unsigned int)&bools.SmplBl_1);
  Serial.println((unsigned int)&bools.SmplBl_2);
  Serial.println((unsigned int)&bools.Bln_1);
  Serial.println((unsigned int)&bools.Bln_2);
 
  while (!Serial.available()) {
  }
  Serial.read();
}

in serial monitor on picture i see

int x = ....
print.sizeof(x)

INT is 2 bytes in adruino system. ESP - 4 bytes.

boolean and bool is the same.

I am aware of that, I posted the OP's original code that was attached as an .ino file in post #3.

I'm using Arduino uno.

Thank you Paul

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.