10

sizeof(char) and sizeof(bool) are both equal to 1 (in my compiler/system/whatever, I've heard that it's not always the same value), a bool can only store true or false while a char can take more values and can act as multiple bool variables using bitwise operators (8 bits, each bit can be used as 1 bool for a total of 8 bools)

So is there any advantage on using bool instead of char?

So aside from readability is there anything else? I've read somewhere that int gets processed faster than short or byte even if takes more memory. Is there any difference in terms of speed between char and bool?

7
  • 10
    Yes: a bool can only store true or false. Commented Nov 3, 2013 at 13:24
  • 4
    Readability: using bool communicates that you intend to only store true or false. Also being precise with your types enables better error messages and better overload resolution. Commented Nov 3, 2013 at 13:25
  • 1
    sizeof(bool) isn't necessarily 1, implementation may choose a bigger size. Commented Nov 3, 2013 at 13:26
  • If there was no bool you would ask why there is no bool and only char. Commented Nov 3, 2013 at 13:29
  • 1
    Well you could theoretically store 8 bools within one char if your system allows it. But intent is the main reason. Commented Nov 3, 2013 at 13:41

2 Answers 2

17

The main point of using bool is to express intent. If a variable is intended to store a value with true/false semantics, allowing for additional values is just a potential source of errors.

Sign up to request clarification or add additional context in comments.

Comments

2

bool is an abbreviation of "boolean", so every one familiar with boolean algebra can be sure that the variable might store only one of the two logical values (true or false): if you're need a variable that could be in only one of these two logical states, is there a reason to use something that could store anything else?

The only size, clearly defined by the standard is sizeof(char), it is 1 byte, but sizeof(bool) differs. What about 1 bit per value, it's about boolean vector template.

[taking an edit into attention]

You've profiled your application and found this to be a bottleneck? As I know, there's no advantages, except using boolean vectors if you're need to store and manipulate a couple of boolean variables.

6 Comments

one advantage that was pointed out to me was that since a char uses 8 bits of memory, it can act as eight boolean variables by using bit-wise operators, basically to save memory space
@KyleRosales, definitely. In many libraries there's also a defines for byte, word, and dword. I'm not the native English speaker, but as for me, it's a kinda weird to shift, mask or bitwise-compare characters and integers :D
@KyleRosales Putting eight booleans in a char does not save memory space. More memory would be used by the machine instructions that would be needed to extract and test each bit.
@KyleRosales: You can save space manually with a lot of work. If you declare 8 bools the compiler can potentially do the same optimization automatically if it wants to (there is probably a reason that this is not an automatic compiler optimization (ie it is not worth the space saving (or it costs in terms of speed)). But the compiler is only going to do if it is always safe. Which is a potential danger of self optimization often overlooks. There are some types that can help std::bitset<8>.
Nope, I've never made a code where using bool became a bottleneck, I was just asking theoretical questions
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.