Question

I have a few pages which contains access bitmasks, which restrict access only to given users. Every user has his own access bitmask, which is used to compare with page bitmask.

For example, I have following page and user bitmasks:

USER 0100
PAGE 0101

To check if user has access to a page, I perform bitwise AND operation between page and user access code. If result of such operation equals to user bitmask, then access is granted.

USER & PAGE == USER   <--- ACCESS OK
USER & PAGE != USER   <--- NO ACCESS

For the example above:

0100 & 0101 == 0100   <--- ACCESS OK

My solution was working great until I have created a user with wider access:

USER 1100
PAGE 0101

1100 & 0101 = 0100   <--- NO ACCESS

The additional bit in user's bitmask ruins my access verification method. How can I fix it?

To grant access, at least one bit must return "true" for AND operation between USER and PAGE bitmasks.

Was it helpful?

Solution

I found the answer for this problem. If at least one bit has to be matched, I can just check if result of the formula is larger or equal than 1: USER & PAGE >= 1 This way, the formula will be true if at least one bit will match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top