November 27, 2020
Using bitmask for permissions is not something new, it’s been widely used in *nix world (linux, macOs, Unix) since forever. I was just curious how it’s actually implemented and wanted to explorer why it’s useful also when it’s useful.
Let’s assume a very simple permission system
Permission | Dec | Binary |
---|---|---|
Execution (x) | 1 | 0001 |
Write (w) | 2 | 0010 |
Read (r) | 4 | 0100 |
Each permission is a bit in the binary position, eg. first bit represent has execution
permission, second bit = has write
permission and so on. so 0111 means have all the 3 permissions, which in decimal is 777
, that’s why chmod 777
gives all the permissions in nix world.
Follow this we can do a very simple permission assignment and check system.
const PERMISSIONS = {
x: 1, // 0001
w: 1 << 1, // 0010 = 2 move 1 position from the left
r: 1 << 2, // 0100 = 4 move 2 positions from the left
};
let userPermission = 0; // initialize without permission
// for permission assignment we just need to do
// granting execution permission.
userPermission |= PERMISSIONS.x // he's 0001
// granting read permission
userPermission |= PERMISSIONS.r // he's now 0101
// how to check permission
if (userPermission & PERMISSIONS.x) {
// & is AND so 0101 AND 0001 returns true
console.log('user has execution permission');
}
if (userPermission & PERMISSIONS.w) {
// now 0101 AND 0010 returns false
console.log('user has write permission')
}
if (userPermission & PERMISSIONS.r) {
// now 0101 AND 0100 returns true
console.log('user has read permission')
}
if you run above code you should see output
user has execution permission
user has read permission
Now the question is why is this and what’s the difference if I do it as an array like
const userPermission = [true, false, true];
or as an object?
const userPermission = {
x: true,
w: false,
r: true,
}
There are many advantages:
const editor = 3 // 0011 read and write only
to determine whether a user is an editor would be fairly straightforward with
if (userPermission & editor ){
console.log('you are editor');
}
doing that with an array or object would require more work…
There are also disadvantages :
Hopefully, this explains why bitwise is better and when we can take advantage and use it