0

What is advantage of using bool data type over integer values as 0 and 1 for true and false values, which will be more efficient to use? And how it will differ in different languages like c and c++?

flamelite
  • 2,654
  • 3
  • 22
  • 42
  • 1
    older versions of C did not have a bool data type – bruceg Sep 22 '16 at 05:56
  • 3
    Possible duplicate of [Which is faster : if (bool) or if(int)?](http://stackoverflow.com/questions/5764956/which-is-faster-if-bool-or-ifint) – Rohith R Sep 22 '16 at 06:04
  • Using `bool` conveys intent, a `bool` value is unambiguously `true` or `false`, while an integer value can take on many more states. This ambiguity could contribute to errors when code is maintained, for example. Performance difference is probably insignificant and if you need to handle so many boolean values that optimization is being considered, you will opt to store values in a bitset, as single bits within an array of integer values. – Christopher Oicles Sep 22 '16 at 06:42
  • This question isn't meaningful and cannot be answered unless you ask it _either_ for C++ _or_ for C. They have very different implementations of `bool` and boolean expressions. Please edit the tags and settle for one language only. Everyone who has answered your question so far either didn't see the multiple tags, or they are simply confused. – Lundin Sep 22 '16 at 06:44
  • @Lundin have extended the question – flamelite Sep 22 '16 at 06:48
  • It is too broad, please just ask one question per question. Now you are asking "what is the advantage of bool in C", "what is the advantage of bool in C++", "how are bool different in C and C++". That's 3 questions. – Lundin Sep 22 '16 at 06:52

4 Answers4

3

int was used in C when there was no bool, there is no advantage1 using an integer type to represent a boolean value in C++ or in modern C:

  1. bool is meaningful, int is not:
bool try_something();
int try_something(); // What does this return?

It is common in C to have functioning returning int to indicate success or failure, but a lot (most?) of these do not return 1 on success and 0 on failure, they follow UNIX standard which is to return 0 in case of success and something else in case of error, so you get code like this:

int close(int fd);

if (close(fd)) { /* Something bad happened... */ }

See close manual which returns 0 on success and -1 on failure. For new user, this code is disconcerting, you expect close to return something that is true if it succeed, not the opposite.

  1. If you need to store large amount of boolean values, you may want to optimize the storage by using only one bit to represent the value (against 32 bits if you store it in a int), and in C++, std::vector<bool>2 is already specialized to do that, so:
std::vector<bool> bvec(8000); // Size ~ 1000 bytes (optimization of std::vector<bool>)
std::vector<int> ivec(8000);  // Size ~ 32000 bytes (32-bits int)

1 There are no advantages if you use them in the same way (e.g. by doing bool b = f(); vs. int b = f(); or the vector<bool> above (if you can replace int by bool without problem in your code, then do so)), but if you use an int to store multiple boolean values using bitwise operations, this is another question.

2 Note that this only applies to std::vector<bool>, other containers such as std::array<bool, N> are not optimized because they cannot use proxy object to "represent" a bit value.

Holt
  • 36,600
  • 7
  • 92
  • 139
  • bool is less storage efficient than an integer of the same size as it always has at least 7 bits of padding, unlike comparable integers. The array argument is irrelevant since vector doesn't use the type bool to implement it, it uses an integer. Why? Because integers are more storage efficient. – 2501 Sep 22 '16 at 07:42
  • I agree with this part: *bool is meaningful, int is not:*. – 2501 Sep 22 '16 at 07:44
  • @2501 *"bool is less storage efficient than an integer of the same size as it always has at least 7 bits of padding, unlike comparable integers."* - Could you elaborate? If you use `uint8_t` (assuming `sizeof(bool) == sizeof(uint8_t)`, and store `0` or `1` inside you will also get 7 bits of padding. If you are talking about storing multiple boolean value on a single `uint8_t`, then my arguments about `std::vector` is relevant since it is exactly what happens. – Holt Sep 22 '16 at 07:49
  • The bool vector isn't using type bool to store boolean values, so this statement is not correct: *bool is more storage efficient, especially when you want to store large array of these (given sizes are possible ones, not mandatory ones):* – 2501 Sep 22 '16 at 07:50
  • @2501 Yes, I know that, but it does change the fact that using `std::vector` to store boolean value is more storage efficient than `std::vector` - I will change the sentence to emphasizes the fact that I am targeting optimized container such as `std::vector`. – Holt Sep 22 '16 at 07:51
  • bool data type != vector of bool, the question is about bool data type. – 2501 Sep 22 '16 at 07:51
  • @2501 Regarding bool, storage efficiency matter if you need to store a lot of them, if you use one bool I don't think it matters that much, which is why I chose to talk about optimized container of `bool` - I am editing the answer. – Holt Sep 22 '16 at 07:54
  • You ask: int try_something(); // What does this return? Answer: It returns 0 or non-zero. just like true or false. – pgibbons Feb 25 '22 at 13:30
2

It's mainly a style issue and so it's hard to prove one way is correct. C allows the syntax if(x) where the condition is executed if x is non-zero. So "true" can be a bit of a trap, if(x == true) doesn't always mean what you think. On the other hand, return true is a lot clearer than return 1 in a function like is_valid(). bool can be more memory efficient but that can be an illusion, often it's padded to four bytes anyway for efficiency reasons.

The main issue with bool, though again it is style issue, is that

mywdgt = openwidget("canvaswidget", 256, 256, true);

obviously means open or create a widget, which is a canvas, and is 256 x 256 pixels. But what is the last parameter?

mywdgt = openwidget("canvaswidget", 256, 256, ALLOW_ALPHA);

is a lot clearer. You know what the parameter is and what it does, at a glance. So bool arguments should be avoided in function signatures, use a define instead and say what the flag means.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
  • true or false is for those that can't comprehend binary in their head. for other people 1 and 0 looks a lot cleaner. – pgibbons Feb 25 '22 at 13:32
0

bool - the built-in Boolean type. A bool can have the values true and false.

So, if you have a scenario in which a boolean type makes sense, e.g., a flag or return value to denote yes/no or success/failure, you would consider using bool.

But then if you have multiple such values, the use of an int(s) would be more appropriate. bool would not be appropriate because the values involved should be restricted to true/false.

Lundin
  • 195,001
  • 40
  • 254
  • 396
msc
  • 33,420
  • 29
  • 119
  • 214
-1

To save memory it is highly recommended to use BOOL e.g if we are just checking condition(true or false) or some thing like that.

Bool occupies only 1 byte(generally- smallest addressable size) of memory whereas integer may take 2 or 4 bytes or more depends on compiler.

So its very much advisable to use bool instead integer to store only 1 or 0. Why to waste memory if you can do it in less memory.

And the bool can guarantee to have values only 0 or 1. So it will be appropriate for conditional statements.

Stubborn
  • 780
  • 1
  • 5
  • 20
  • 2
    sizeof(bool) is not required to be 1 – abhiarora Sep 22 '16 at 06:16
  • 1
    @abhiarora I've written generally man. – Stubborn Sep 22 '16 at 06:17
  • 3
    Memory consumption is not the reason why you should use bool in C nor in C++. If it was the reason, you would use a bit field with just one bit set to 0 or 1 instead of wasting 8 bits. Booleans aren't guaranteed to be implemented as bits, so the memory consumption argument is nonsense. – Lundin Sep 22 '16 at 06:49
  • In most circumstances even if bool is 1B padding will be added or for a function call, the value will be promoted to int. Unless you're making an array of bool, there is no significant memory impact. – martinkunev Apr 04 '19 at 09:43