Does this account hold every right
Access control packs rights into a bitfield. A guard checks whether an account holds all of the rights an action needs.
- Both values are bitfields: a set bit means that right is present or required.
- Return true only when every required right is also held.
- Requiring nothing is always satisfied.
- Extra rights the account holds do not matter.
HoldsEveryRight(held: int, needed: int) → bool
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public bool HoldsEveryRight(int held, int needed) {
}
Worked examples
| Call | Result |
|---|---|
HoldsEveryRight(7, 5) | true |
HoldsEveryRight(5, 7) | false |
HoldsEveryRight(0, 0) | true |
HoldsEveryRight(8, 0) | true |
Hint
Mask what is held against what is needed. If the masked result still equals what was needed, nothing was missing.
Reference solution in C#
public bool HoldsEveryRight(int held, int needed) {
return (held & needed) == needed;
}