Bitwise AND in a condition

Hi,

for my enumeration, I need to check the integer sum with a bitwise AND.
1
2
3
4
class myClass
{
enum myEnum { intOne = 1, intTwo = 2, intThree = 4, intFour = 8};
};

It works all fine like this:
1
2
3
4
5
6
7
void checkEnum(int intEnum)
{
  if((intEnum & myClass::intOne) > 0)
    ...
  if((intEnum & myClass::intTwo) > 0)
    ...
}

What I don't understand is, why the following also works:
1
2
3
4
5
6
7
void checkEnum(int intEnum)
{
  if(intEnum & myClass::intOne)
    ...
  if(intEnum & myClass::intTwo)
    ...
}
I mean, as a result of the bitwise AND, I get an integer with the size of the enum, but not a boolean. So why can I check for true and false?

Thank you in anticipation.

Binary
Last edited on
When a numeric type is given as the condition, you could imagine that there is an implicit != 0 added to the end of the expression.
if checks for a numeric value != 0, not for bool.
Ah that is it.. Thank you!
Topic archived. No new replies allowed.