3

We all know C++ (while not a superset) is pretty much derived from C.

In C++, the operators <, <=, >, >=, ==, and != all have boolean return values. However, in C, the same operators returned 1 or 0, since there was no 'bool' type in C.

Since all integer values except 0 are treated as "true", and 0 is "false", I want to know:

Does C++ still restrict the return values of the operators to be 1 vs 0, or does a 'true', from one of these operators, return any 1-byte value, so long as it isn't 0?

I want to know since using these return values as explicit 1 or 0 would be useful in bitwise operations without branching.

As a terrible example, take the following:

bool timesTwo;
int value;

//...

if(timesTwo)
    value << 1;

//vs

value << (int) timesTwo;
6
  • 1
    I dont understand what youre asking, C++ operators return a boolean, not an int. Oct 26, 2012 at 21:37
  • 1
    your example is fine, the int cast will transform true into 1, or false into 0.
    – qdii
    Oct 26, 2012 at 21:39
  • Is your question equivalent to the question of whether int a(5); assert( (int)((bool)a) == 1 ); will always be ok? Oct 26, 2012 at 21:42
  • 1
    @MattPhillips Somewhat. Let us ASSUME for a second that casting an int, to a bool, back to an int, retains its original value, such that assert( (int)((bool)(5)) == 5 ). Under THAT assumption, my question was actually asking something more along the lines of whether int x; assert( ((int)( x == x )) == 1); would hold, which gives question to the actual byte returned by the x == x comparison. However, since casting to an int is always 0 or 1, the question becomes (almost) meaningless.
    – Serge
    Oct 26, 2012 at 21:47
  • Or, you could say that the question is asking if the following holds: bool b = (5 == 5); char* cp = (char*) (&b); assert( (*c) == ((char) 1) );
    – Serge
    Oct 26, 2012 at 21:49

3 Answers 3

5

Does C++ still restrict the return values of the operators to be 1 vs 0, or does a 'true', from one of these operators, return any 1-byte value, so long as it isn't 0?

The comparison operators, assuming that they have not been overloaded, only ever return true and false.

int(true) is always 1.

int(false) is always 0.

So,

int one(1), two(2);
assert( (one<two) == 1 );
assert( (two<one) == 0 );
5
  • 1
    Thanks, Rob. While my question was more referring to the actual byte value that boolean holds (since it's never just 1 bit), I believe this provides the necessary clarification.
    – Serge
    Oct 26, 2012 at 21:42
  • 4
    The standard is silent on that point. You can't know what byte value the boolean holds, but you can convert it to an int and inspect that.
    – Robᵩ
    Oct 26, 2012 at 21:47
  • Copied from one of my comments above, you could say that the question is asking if the following holds: bool b = (5 == 5); char* cp = (char*) (&b); assert( (*c) == ((char) 1) );
    – Serge
    Oct 26, 2012 at 21:50
  • 4
    Practially, yes, that holds for every copmiler you are going to use. But, I'm not so sure it holds true in the standard. The standard language is all about conversions to/from integer types, and not about at all about representation.
    – Robᵩ
    Oct 26, 2012 at 23:06
  • Ok, thanks Rob! Appreciate it! While you originally gave me an answer to my main issue, out of curiosity I still wanted to know if something like that was defined; Thanks for all the answers =)
    – Serge
    Oct 27, 2012 at 4:03
2

In fact, bool is automatically converted into integer in C++ (1 if true, 0 if false) when used in expressions and there's no need to cast.

value << (int) timesTwo;

The case is not necessary: if `timesTwo`` is true then

you can directly do without a casr:

value<<timesTwo;

which is equivalent to:

value<<1;
0

The type bool has two values: false and true. You can treat it as an integer, in which case, false is converted to 0 and true is converted to 1. That's all there is to it.

The C-style sort-of-boolean, where 0 is treated as false and non-zero is treated a true, leads to problems when someone naively does something like #define FALSE 0 and #define TRUE !FALSE. That effectively defines TRUE as 1, and comparisons like if (f() == TRUE) can mysteriously fail. The correct test would be if (f() != FALSE). Of course, with a real boolean type, that's not an issue, because values that aren't false will always be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.