How can I compare a enum to a variable if the enum contains mulitple possible values?
Ex: for the following enum
typedef enum {
EnumValueA = 2,
EnumValueB = 3,
EnumValueC = 4,
EnumValueD = (0 | -1)
} EnumValues;
When comparing a variable like BOOL result = (a == EnumValueD) I get NO if int a = 0 and YES if int a = -1.
Also, this enum is inside a component so I just can’t change it..How
How can I make this comparison return YES for both 0 and -1?
Your question is based on a misunderstanding. The line
does not indicate “multiple values”. That is a “bitwise-or” operator: it performs an operation (bitwise) on the numbers zero and minus one, and yields just one answer. It’s a curious bit of code (probably having to do with integer byte size issues), but you only need to test against -1. (If you really want to be safe you could test against
(0|-1), I suppose.)Moral: Don’t confuse logical-or
||with the bitwise-or operator|.