Skip to content

Instantly share code, notes, and snippets.

@malisetti
Last active June 8, 2021 03:44
Show Gist options
  • Save malisetti/ddd98e0225c9211278e547b7c4d58bd1 to your computer and use it in GitHub Desktop.
Save malisetti/ddd98e0225c9211278e547b7c4d58bd1 to your computer and use it in GitHub Desktop.
typescript enum bitmasks
https://www.typescriptlang.org/play?#code/PTAEGUFMGcAsEMDGCA28C2oC0oAuBPABxkQCcBLQ3UAI3N3XmgGtoBYAKE8gDsBXTAGEASgFUAIqADenUHNCDSkeLkigAvKACMoADy7QABgA0s+cOUATDdr0GtpjvNCjCllWs079oAEyPncUgUSFUbbwMAZkcAX05ORAB7HmhqAEEUFBEJAHkqcmToG2zxADpFZTCAHwUxMot4axqS0td3atqJUqCQ1QBueI4klMSQ0pREgHMACgysurzcApSASgGuIcLqeEzEgHdLReWizQBtFoqPY06yto8AXXXOXtp6E6N1gDNE0lBp4dSoESQM+oB2EwOR0KK2kZjkdFwRSqmkS6ziGwBo0g4ym0wR0DWg0xYwmMzxb1AADIbuUlB4YepGTTLqpCZsRiTceTEVSaXdWRomS1+ZA2Zx8VTNAA-YVuDxPdnQLE4mb4sWK5Wk6bcorUi50gWMzT6yqi9bE7FanW82XtUWC411Vpy1nrIA
// Seshachalam - typescript bitmasks
enum CRUD {
Create = 1 << 0,
Read = 1 << 1,
Update = 1 << 2,
Delete = 1 << 3,
}
const AllCRUDOptions = CRUD.Create | CRUD.Read | CRUD.Update | CRUD.Delete;
console.log(AllCRUDOptions);
const allowdOptions = [CRUD.Create, CRUD.Update];
let bits = 0;
for (const o of allowdOptions) {
bits |= o;
}
console.log(bits);
console.log((bits & CRUD.Create) === CRUD.Create);
console.log((bits & CRUD.Update) === CRUD.Update);
bits &= ~CRUD.Update;
console.log(bits);
console.log((bits & CRUD.Create) === CRUD.Create);
console.log((bits & CRUD.Update) === CRUD.Update);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment