3

If I have the following code:

int a = 1;
bool b = 1;

Does a equal to b? Even the program might return that they are the same, are they actually equal in all aspects in low level?

Also, if I use code (pseudo) such as:

if (a)
then execute();

will execute() run? I am asking for theoretical answers, and I can't convince myself with experiments as this is not natural science. Thank you all.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
return 0
  • 4,226
  • 6
  • 47
  • 72

4 Answers4

10

I think you can convince yourself with the right experiments:

#include <type_traits>

int main() {
    int a = 1;
    bool b = 1;
    static_assert(! std::is_same<decltype(a), decltype(b)>::value,
                  "No, they are not the same on all aspects");
}

Perhaps the most important difference between the two is that bool can only have two values: true and false, while int can have many more. Here's another experiment that shows a consequence of this:

#include <cassert>

int main() {
    int a = 2;
    bool b = 2;
    assert(a != b);
}

The two types may seem similar because there are implicit conversions between the two. Any integral expression that is zero can be implicitly converted to false, and any integral expression that is not zero can be implicitly converted to true. In the opposite direction, false can be implicitly converted to zero, and true converted to one. This leads to the code above ending up testing if 2 != 1.

Now the answer to the question of whether execute(); is called in the snippet from the question should be obvious: the value a will be converted to a bool in the if statement, and since it is not zero, it will convert to true and result in a call to execute().

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • Is this supposed to help someone? If the OP is asking the question in the first place, do you think it likely that he will understand this bare piece of code? I know I don't... – Ned Batchelder Jul 06 '12 at 01:07
  • @NedBatchelder This code is literally asking the compiler if `a` and `b` are the same type. If you compile it, it will answer the OP's question. – Brendan Long Jul 06 '12 at 01:09
  • @NedBatchelder, `std::is_same` checks if two types are the same, and `decltype` gives the type. `static_assert` is a compile-time `assert`. – chris Jul 06 '12 at 01:14
  • @Ned I expanded the answer a bit. The other folks have already explained the bits in the code, so I'll just thank them instead of echoing them :) – R. Martinho Fernandes Jul 06 '12 at 01:36
5

A non-zero numerical or pointer expression will evaluate to true when used in a boolean context. Similarly, a zero expression will evaluate to false. However, an int and a bool are different types, so they are not the same.

jxh
  • 69,070
  • 8
  • 110
  • 193
4

Does a equal to b? Even the program might return that they are the same, are they actually equal in all aspects in low level?

It all depends on what you mean by equal. The type is different, and that means that the representation in memory will probably differ (the compiler is free to represent those two as exactly the same, but it is also free to do otherwise). In most compilers/architectures, bool takes just one byte of storage and int has a bigger size (usually 4 bytes, but this depends on the architecture).

Besides the different sizes, the compiler will treat both types differently (not just the loads and stores to memory, but also the operations will be different. You can only store 0 and 1 in a bool, and that means that some operations might use that knowledge. For example, in this article you will find one case where the implementation of the test of a condition differs (note, the article has a case of undefined behavior that causes a bool to evaluate to both true and false as for the test the compiler is assuming that the bool can only be either 0 or 1, that cannot happen with int)

From a logic point of view, the language determines how the different types are used in operations, and in particular if you try to compare a and b in your program, the result of the expression will be true. Note that it does not mean that they are exactly the same, the language defines a set of conversion rules that are used to transform both variables into the same type and the comparison is performed in that type. In this case the conversion will be to int. The bool variable will be converted to 0 if false or to 1 if true.

Also, if I use code (pseudo) such as: if (a) execute(), will execute() run?

Yes. In this case, the condition inside the if requires a bool value, so the conversion will be from int to bool. The standard defines that conversion to yield false if the integer value is 0, or true otherwise, effectively doing the equivalent of if (a!=0). Since a is 1, the condition holds and execute() will be evaluated.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

This thread talks about c++ booleans in comparison to chars, but it'll give you a good idea of what's going on under the hood. Why is a char and a bool the same size in c++?

Community
  • 1
  • 1
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103