Assuming true equals 1

Started by
16 comments, last by Kest 18 years, 6 months ago
I hate asking dumb questions, but this has me curious. I use C/C++ with the MSVC.net compiler, and my program will be compiled with it only. I'm not so much worried about different compiler behavior, but any information about such things would also be interesting. Anyway, my concern isn't whether the defined value for 'TRUE' or 'true' is 1. It's whether the return values of a bool statement is always 1. Here are some examples: int value = ( number < max ); int value = ( max < number ); int value = ( number & max ); // I know this will not always be 1 or 0 - but not really a bool statement int value = ( number == max && max > 6 ); Is it safe to assume a 1 or 0 with most conditional statements? This is what I'm currently doing in my code, after some conditional statements, and I'm wondering how dumb this type of thing looks: value = ( value ) ? 1 : 0; Thanks for any information.
Advertisement
According to the C++ standard (Section 4.7 paragraph 4 for those who care), during integral conversions, true is always converted to 1 and false is always converted to 0.
In C++ bool is guaranteed to be 0 or 1 and boolean expressions yield bool's as results.


value = ( value ) ? 1 : 0;

You need something like that if you want to guarantee that value is 1 or 0 and value is not a bool.
Here's a method without any branches:
value = !!vaule;
or
value = (bool)value;
ought to work too.

If value is a bool then it's already 0 or 1.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks for the quick replies. I really appreciate it :)
I make only the assumptions that true == true, false == false, and true != false. Under C/++, I also assume that integer 0 is considered "false". But I make no bets or assumptions about "true" having any specific value. I've been bit by this under at least one machine language and one VM, so I make no assumptions about how higher-level languages treat it.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Note that while the expression (foo) is equivalent to the expression (foo ? 1 : 0), I tend to prefer the latter. It makes it more clear that a boolean mapping is occuring and that the result is an integer. Terseness is often counterproductive in C++.
Quote:Original post by Sneftel
Terseness is often counterproductive in C++.


Sadly, this is true.
Just a small consideration that's easily missed: bool and BOOL aren't the same. BOOL is probably an integral type, and can hold more than just 0 and 1. Similar, TRUE and FALSE are probably not booleans, but integers as well.

CM
Quote:Original post by Conner McCloud
Just a small consideration that's easily missed: bool and BOOL aren't the same. BOOL is probably an integral type, and can hold more than just 0 and 1. Similar, TRUE and FALSE are probably not booleans, but integers as well.

CM


A simple example is the GetMessage() WinAPI function:
Quote:
BOOL GetMessage(LPMSG, HWND, UINT, UINT);

- If the function retrieves a message other than WM_QUIT, the return value is nonzero.
- If the function retrieves the WM_QUIT message, the return value is zero.
- If there is an error, the return value is -1.
"after many years of singularity, i'm still searching on the event horizon"
In general, you should not need to make such an assumption, and you may make your code harder to maintain by doing so. Also, although this may work in C++, you won't be able to get away with it in a number of other languages. Even in C++ you have to be weary of interfacing with other languages, as true is -1 in VB for example! (See the definition of VARIANT_TRUE in WTYPES.H. I think there's a vTRUE of -1 as well in .NET)

The only thing that really is safe to assume, is that false == 0 and true != 0, and you'd be best to write any expressions in that manner if possible.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement