I'm reading The C++ Programming Language. In it Stroustrup states that sizeof(char) == 1
and 1 <= sizeof(bool)
. The specifics depend on the implementation. Why would such a simple value as a boolean take the same space as a char?
Character refers to a single letter within a string (words). Boolean is either a True or False value.
However, my C++ book (C++ Pocket Reference, O'Reilly) states: "The typical size of a bool is one byte," and "The size of a char is one byte. The size of a byte technically is implementation defined, but it is rarely anything but eight bits."
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
In modern computer architectures, a byte is the smallest addressable unit of memory. To pack multiple bits into a byte requires applying extra bit-shift operations. At the compiler level, it's a trade off of memory vs. speed requirements (and in high-performance software, those extra bit-shift operations can add up and slow down the application needlessly).
Because in C++ you can take the address of a boolean and most machines cannot address individual bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With