Go Down

Topic: booleans and memory (Read 404 times) previous topic - next topic

mattgilbertnet

Quick question. How much memory does a boolean take up? Seems simple: 1 bit. I'm not familiar with C, but if arduino's programming is built on C, which "does not have a distinct boolean type -- int is used instead", does that mean that every boolean in arduino code is actually and int and takes up 32 bits? What about boolean arrays?

kg4wsv

Here's a trick:

Code: [Select]
Serial.println(sizeof(boolean), DEC);
If you run this you'll find that booleans are 1 byte each.  int on the AVR is 2 bytes, not 4.

sizeof() is a C builtin that tells you the size (in bytes) of a type.

The smallest addressable unit is a byte.  If you want to try tricks like storing an "array" of 8 bits in a byte you can do so, but the AVR (and every other architecture I've ever worked on or read about) uses at least 1 byte per element.

-j

Go Up