Created
December 14, 2019 17:26
-
-
Save SupertigerDev/b2ee98e3e2eb374cbdab04eb8b40cf88 to your computer and use it in GitHub Desktop.
bitwise permissions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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