Question

Consider the following code snippet:

main()
{
    bool flag = true;             //line 1
    flag &= funcReturningBool();  //line 2
    flag &= funcReturningBool2();
    flag &= funcReturningBool3();
    //....
    //....
    //....
    //so many such cases
}

bool funcReturningBool()
{
    bool ret = false;
    // my logic which may (not) modify ret
    return ret;
}

bool funcReturningBool2()
{
    bool ret = false;
    // my logic which may (not) modify ret
    return ret;
}

bool funcReturningBool3()
{
    bool ret = false;
    // my logic which may (not) modify ret
    return ret;
}

The static code analyzer tool points out the following issue (on line 2):

"Bitwise operator is being applied to a signed type. The resulting value may not be as expected."

Can someone point out if I'm doing something wrong? Also prescribe some useful/logical alternative methods to achieve the same!

Was it helpful?

Solution

You just shouldn't use a bitwise operator on Boolean values. Apparently the compiler promotes the output of funcReturningBool or the variable flag to a signed integer, and warns you of possible unexpected effects.

You should stick to Boolean operators. Had &&= existed, you could have written flag &&= funcReturningBool();. Instead, use flag = flag && funcReturningBool();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top