Pages: [1]   Go Down
Author Topic: booleans and memory  (Read 386 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a trick:

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

Pages: [1]   Go Up
Jump to: