Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 901
UFO Avatar asked Sep 07 '14 01:09

UFO


2 Answers

I would prefer this one:

BOOL testMode = (BOOL)flag || (BOOL)prod || (BOOL)test;
like image 33
pleshis Avatar answered Sep 28 '22 17:09

pleshis


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.

like image 71
godel9 Avatar answered Sep 28 '22 17:09

godel9