# Assuming true equals 1

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Thanks for the quick replies. I really appreciate it :)

##### Share on other sites
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.

##### Share on other sites
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++.

##### Share on other sites
Quote:
 Original post by SneftelTerseness is often counterproductive in C++.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Conner McCloudJust 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.

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015444
×