Permissions with Bitwise Operators
A set of yes or no permissions can be stored in a single number, where each permission owns one bit. Bitwise operators then grant, revoke and test permissions without an array or an object of booleans. The same pattern, often called bit flags, appears in file permissions on Unix, in game state and in many browser and Node.js APIs. Bitwise Operators & Binary Numbers explains the operators themselves.
One bit per permission
Section titled “One bit per permission”Each permission is a power of 2, so each one has a single 1 in a different column:
const VIEW = 1; // 0001const COMMENT = 2; // 0010const EDIT = 4; // 0100const SHARE = 8; // 1000A user who can view and edit has the bits for 1 and 4 set, which is 0101, or 5. Every combination of the four permissions has a different number between 0 and 15, so the number alone says exactly what the user may do.
Granting, testing and revoking
Section titled “Granting, testing and revoking”| (OR) sets a bit, & (AND) tests one, and & ~ (AND with NOT) clears one:
const VIEW = 1; // 0001const COMMENT = 2; // 0010const EDIT = 4; // 0100const SHARE = 8; // 1000
let access = VIEW | COMMENT;console.log(access);
access |= EDIT;console.log(access, access.toString(2));
console.log((access & EDIT) !== 0);console.log((access & SHARE) !== 0);
access &= ~COMMENT;console.log(access, access.toString(2));This prints:
37 111truefalse5 101Granting a permission the user already has changes nothing, because OR leaves a bit that is already 1 at 1. Revoking one they do not have also changes nothing, because ~COMMENT has a 0 only in the comment column, and ANDing with it clears that one bit and keeps every other.
XOR toggles
Section titled “XOR toggles”^ (XOR) flips a bit, so it turns a permission on if it is off and off if it is on:
const VIEW = 1;const EDIT = 4;
let access = VIEW;access ^= EDIT;console.log(access);access ^= EDIT;console.log(access);This prints:
51That makes XOR right for a toggle switch, but wrong for “remove this permission”, since calling it on a user who lacks the permission grants it. Use & ~ to revoke, so the result does not depend on the starting state.
A small class
Section titled “A small class”The operators read better behind named methods. Here the permissions live in a frozen object, so their names can also be listed:
const Permission = Object.freeze({ VIEW: 1, COMMENT: 2, EDIT: 4, SHARE: 8,});
class Member { constructor(name, access = 0) { this.name = name; this.access = access; }
grant(...permissions) { for (const p of permissions) this.access |= p; }
revoke(...permissions) { for (const p of permissions) this.access &= ~p; }
can(permission) { return (this.access & permission) === permission; }
list() { return Object.keys(Permission).filter((name) => this.can(Permission[name])); }}
const priya = new Member('Priya', Permission.VIEW);priya.grant(Permission.COMMENT, Permission.EDIT);priya.revoke(Permission.COMMENT);
console.log(priya.access);console.log(priya.can(Permission.EDIT));console.log(priya.can(Permission.SHARE));console.log(priya.can(Permission.VIEW | Permission.EDIT));console.log(priya.list());This prints:
5truefalsetrue[ 'VIEW', 'EDIT' ]can() compares the masked bits with the whole argument, so can(VIEW | EDIT) is true only when the user has both. Comparing with !== 0 instead would ask whether the user has either of them.
Because the bitwise operators work on 32-bit integers, one number holds at most 32 permissions. The 32nd bit is the sign bit, so a user who has that permission has a negative access value. The tests above still work, but the first 31 are easier to read when printed. The number is easy to store in a database column or send in a token.
Steve Griffith covers this topic in his video Managing User Permissions with Bitwise Operators.