Search code examples
iosxcodecocoacocoa-touch

Use of logical || with constant operand


I have a few defines like this:

#define flag   YES
#define prod   YES
#define test   NO

these are used for tests.

At one point of code I have

BOOL testMode = flag || prod || test;

Xcode whines with this message: use of logical || with constant operand... fix it using bitwise

but the operation I am doing is logical, not bitwise.

I want testMode to be YES if one of the 3 states are YES.

Any clues?


Solution

  • The following code gets the same result without the warning.

    BOOL testMode = flag | prod | test;
    

    It may be a matter of what would bother you more... an unnecessary warning or using the bitwise | operator for what's essentially a logical operation. The warning seems designed to catch people incorrectly using the logical operators with bit fields. You wouldn't want to accidentally write bitField || 0x4 when you're trying to set bit 2.