1

I have objects, which I only want to display to the visitor based on different criteria. The object has a bitmask and I have defined the following conditions:

const FLAG_ALWAYS   = 0; // always show this item
const FLAG_LOGIN    = 1; // only display to logged in users
const FLAG_NOTLOGIN = 2; // only display to users not logged in
const FLAG_OTHER    = 4; // other criteria
const FLAG_NORTH    = 8; // GeoIP
const FLAG_SOUTH    = 16;

Combinations of flags are possible of course like 1+4+16 or 2+4.

An item can be displayed under 3 conditions for login for example: logged in, not logged in, or both. Therefore I need FLAG_NOTLOGIN.

I'm confused by the FLAG_ALWAYS... should it be 0, or should it cover all other flags like 4095 ?

Or should I remove FLAG_NOTLOGIN ?

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • When more than one flag set, condition is conjunction of flags (all criteria met), or disjunction (any criteria met)? – Vovanium Apr 29 '14 at 11:55
  • @Vovanium All of the users flags must be present in the objects flag. A user can have flag `FLAG_LOGIN` and an object can have `FLAG_LOGIN` and `FLAG_NOTLOGIN` – Daniel W. Apr 29 '14 at 12:03

2 Answers2

1

FLAG_ALWAYS should be combination of all other flags and it should not be zero. FLAG_NOTLOGIN need not be removed.

radja
  • 11
  • 1
1

Answer depend on how you combine criteria. There are two most simple cases.

Any match, OR combination. any flag you set will add matches, more flags more matches.

In this case all flags reset (0x0000) will match never. This means no criteria met.

All flags set (0xFFFF) will cause most matches. In case you have complimentary criteria (one of them is set) criteria will match always.

Match is implemented like this: 0!=(filter & criteria) where filter is set of criteria to filter and criteria is set of same flags set on several conditions.

All match, AND combination. any flag you set will filter out some matches, more flags less matches.

In this case all flags reset will match always.

All flags set will cause least matches. If you have mutually exclusive criteria (one set then others reset) when all ones will cause no matches.

E. g. your flags: FLAG_LOGIN, FLAG_NOLOGIN. User may be either login or not, so BOTH criteria will never met, and FLAG_LOGIN+FLAG_NOLOGIN will never match, but 0 will match in any case as none of criteria set.

Match is implemented using this formula: 0==(all_flags & ~filter & ~criteria), here filter and criteria are same as above and all_flags is set of all used flags, to exclude unused bits in comparison. (note, expression criteria == (filter & criteria) is seem more obvious but wrong, because it will cause no matches when no flags ar set in criteria).

If your flag is 1 | 4 and object has 1 | 8, then first case will have match (because 1 criteria met and one is sufficient) and second case will not have match (4 criteria does not met but you need both 1 and 4).

Vovanium
  • 3,798
  • 17
  • 23
  • When the user has flag (example) `1 | 4` ( = 5), and the object has `1 | 2 | 4 | 8` ( = 15) then `is user allowed to see the object? (5 & 15) == 5` evaluates to true, user is allowed to see the object. So my case is the first one :-) I think it's a missunderstanding but your explanation is good and correct!! – Daniel W. Apr 29 '14 at 12:36
  • Umm, see if your flag is `1 | 4` and object has `1 | 8`, then first case will have match (because `1` criteria met and one is sufficient) and second case will not have match (`4` criteria does not met but you need both `1` and `4`). That is the difference – Vovanium Apr 29 '14 at 12:41
  • `(5 & 9) !== 5` so the user is not allowed to see the object. If the user has `1 | 4` and the object has `1 | 4 | 8` then the user is allowed to see the object. This is the expected logic. – Daniel W. Apr 29 '14 at 12:47