Skip to main content
gamedev.net
🔒 Locked

Bool type and operators

Started by rikibee Aug 16, 2006 at 1:07 PM 1 replies 750+ views
Original Post
rikibee
rikibee
Hi everyone, Lets say I have some functions that returns a boolean:

bool InitA();
bool InitB();
After reading the C++ spec draft, I still cannot tell if this would be correct:

bool result = true;
result &= InitA();
result &= InitB();
return result;
Is this correct or not? I know that the following would be correct:

bool result = true;
result = InitA() && result;
result = InitB() && result;
return result;
Is operator &= a bitwise AND operator, even for bool type? (b1 = b1 & b2 ). If so, does true have always the same bit representation for a given compiler? I know MSVC give true the value 1, but is this a standard?
Conner McCloud
Conner McCloud
Quote:
Original post by rikibee
Hi everyone,

Lets say I have some functions that returns a boolean:
bool InitA();bool InitB();


After reading the C++ spec draft, I still cannot tell if this would be correct:
bool result = true;result &= InitA();result &= InitB();return result;

The section you are interested in is 5.17. The answer is yes.
Quote:

Is operator &= a bitwise AND operator, even for bool type? (b1 = b1 & b2 ). If so, does true have always the same bit representation for a given compiler? I know MSVC give true the value 1, but is this a standard?

I do not believe that true has a guarenteed bit pattern, but I know (int)true is guarenteed to be 1. The end result is the same...your code will behave the way your intuition says it should.

CM
Xai
Xai
Bitwise operators follow boolean logic when operating on 2 boolean operands. Mixing operand types in bitwise operators is a sure way to get totally unexpected results.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.