Drill

ProblemsPython › patterns

Does this account hold every right

easypatternsBit manipulationPython

Access control packs rights into a bitfield. A guard checks whether an account holds all of the rights an action needs.

holds_every_right(held: int, needed: int) → bool

Solve it in the editor →

Where you start

def holds_every_right(held: int, needed: int) -> bool:
    

Worked examples

CallResult
holds_every_right(7, 5)True
holds_every_right(5, 7)False
holds_every_right(0, 0)True
holds_every_right(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 Python
def holds_every_right(held: int, needed: int) -> bool:
    return (held & needed) == needed

The same problem in another language

More patterns problems in Python