Skip to content

Instantly share code, notes, and snippets.

@chibaye
Forked from prof3ssorSt3v3/bitwise.js
Created June 23, 2021 15:30
Show Gist options
  • Save chibaye/1eea55f2124f159d9d3f286d257c6b9b to your computer and use it in GitHub Desktop.
Save chibaye/1eea55f2124f159d9d3f286d257c6b9b to your computer and use it in GitHub Desktop.
/**
* Using a single integer to represent multiple permissions
* based on binary values using bitwise operators
*
* & bitwise AND - if both the top and bottom bit are 1, result is 1
* | bitwise OR - if either the top and bottom bit or both are 1, result is 1
* ^ bitwise XOR - if only one of the bits are 1, result is 1
* 0101
* 0100 & = 0100
*
* 0100
* 1110 | = 1110
*
* 0101
* 0001 ^ = 0100
*
* 0 - 0000
* 1 - 0001 x
* 2 - 0010 x
* 3 - 0011
* 4 - 0100 x
* 5 - 0101
* 6 - 0110
* 7 - 0111
* 8 - 1000 x
* 9 - 1001
* 10 - 1010
*/
const READ = 1; // 0001
const DRINK = 2; // 0010
const SING = 4; // 0100
const DELETE = 8; // 1000
class Person {
constructor(name, access = 0) {
this.name = name;
this.access = access;
}
getAll() {
return {
[READ]: !!(this.access & READ),
[DRINK]: !!(this.access & DRINK),
[SING]: !!(this.access & SING),
[DELETE]: !!(this.access & DELETE),
};
}
addPerm(perm) {
this.access = this.access | perm;
}
removePerm(perm) {
if (this.getAll()[perm]) {
this.access = this.access ^ perm;
}
}
}
let steve = new Person('Steve', 5);
let joanne = new Person('Joanne');
joanne.addPerm(DRINK);
joanne.addPerm(SING);
joanne.addPerm(DELETE);
steve.addPerm(SING);
joanne.removePerm(READ);
steve.removePerm(READ);
console.log(steve.access, steve.getAll());
console.log(joanne.access, joanne.getAll());
// console.log(steve.getAll()[READ]);
// console.log(joanne.getAll()[READ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment