Home > Net >  How do we do bool operations without compiler warnings
How do we do bool operations without compiler warnings

Time:07-21

I have two bools, i want to some booleans operations. (And, Or)
Example:

bool tellStatusOk() 
{
  bool res1 = IsRunning();  // a test funtion which returns ok/fail
  bool res2 = IsActive();
  return res1 & res2;
}

But the VC complains it is a lnt-logical-bitwise-mismatch int-logical-bitwise-mismatch.
I don't want do use && as supposed. Because it make no sense for me for booleans data type.

CodePudding user response:

Without appropriate &&, you might do:

bool tellStatusOk() 
{
    bool res1 = IsRunning();
    bool res2 = IsActive();
    return (static_cast<int>(res1) & static_cast<int>(res2)) != 0;
}

but, following seems fine

bool tellStatusOk() 
{
    return IsRunning() && IsActive();
}

CodePudding user response:

I don't want do use && as supposed. Because it make no sense for me for booleans data type.

It (using &&) makes perfect sense to me.

How do we do bool operations without compiler warnings

Just use && instead of &.

  • Related