Skip to content

Instantly share code, notes, and snippets.

@SupertigerDev
Created December 14, 2019 17:26
Show Gist options
  • Save SupertigerDev/b2ee98e3e2eb374cbdab04eb8b40cf88 to your computer and use it in GitHub Desktop.
Save SupertigerDev/b2ee98e3e2eb374cbdab04eb8b40cf88 to your computer and use it in GitHub Desktop.
bitwise permissions example
// list of permissions
const ADMIN = 2;
const SEND_MESSAGE = 1;
// my current permission
let myPerms = 0;
console.log(containsPerm(ADMIN))
//addPerm(ADMIN)
addPerm(SEND_MESSAGE)
console.log(containsPerm(ADMIN))
addPerm(ADMIN)
//removePerm(ADMIN)
console.log(containsPerm(ADMIN))
console.log(myPerms)
function removePerm(flag) {
myPerms = myPerms &= ~flag;
}
function addPerm(flag) {
myPerms = myPerms | flag;
}
function containsPerm(flag) {
return myPerms & flag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment