Reputation: 121
I want to check if a flag is set in my enum value. What is wrong with my code?
Javascript-Code
var flags = {
FOO: 1,
BAR: 2,
BAZ: 4,
FUM: 8,
ERROR: 65
}
var value = flags.FOO | flags.BAR;
if (value & flags.ERROR){
alert("ERROR IS SET, but this is not true");
}
Upvotes: 1
Views: 5113
Reputation: 3367
First yours values should be n^2:
var flags = {
UNKNOWN: 0,
FOO: 1,
BAR: 2,
BAZ: 4,
FUM: 8,
ERROR: 16
}
Then your statement should be:
if ((value & flags.ERROR) == flags.ERROR){
alert("ERROR IS SET");
}
Upvotes: 3