slicks1
slicks1

Reputation: 349

Simple bitmasking assistance

I have the following flags:

- edit profile => 1
- update coupon => 2
- update news articles => 4

I want to understand how can I set and check the bit mask for all the three flags (that is, the user has access to all 3 bit masks).

Not quite sure what to do here.

Upvotes: 2

Views: 38

Answers (1)

Chris
Chris

Reputation: 5886

If you want a user to have all the permissions then or the bits.

e.g.

$permission_bitmask = $edit_profile | $update_coupon | $update_news_articles;

For just two of the permissions, it's the same idea.

$permission_bitmask = $edit_profile | $update_coupon;

To check the bitmask, and the bitmask with your desired bit.

$has_edit_profile = ($edit_profile & $permission_bitmask) !== 0;

Upvotes: 2

Related Questions