Bitmask
This module enables easy manipulation and usage of bitmasking technique in JavaScript.
What is bitmask?
A bit mask is a binary number or a bitmap where the desired bit(s) are one and the remaining 0. By performing a bitwise
AND operation of a value with a bitmask, one can test for certain bits being on.
Using a bitmask, it is very easy to store the boolean status of multiple flags in a single variable (of higher base.) A
very common use case is to store "permission" value for an object. For example, whether an entity has read or write
permission to an object can be determined easily by storing two flags "read" and "write" within a single variable.
More details on this detailed Wikipedia article on Bitmasking.
Installation
Installation can be done using npm for using in NodeJS applications.
npm install generic-bitmask --save-dev;
Usage
var Bitmask = require('generic-bitmask').Mask;
var mask = new Bitmask();
mask.add(3);
mask.add(5);
console.log(mask.test(3));
console.log(mask.test(4));
mask.remove(5);
console.log(mask.test(5));
console.log(mask.get());
mask.set(40);
console.log(mask.test(3));
console.log(mask.test(4));
console.log(mask.test(5));
console.log(mask.test([3, 5]));
Using the mask descriptor
var Bitmask = require('generic-bitmask').Mask,
Descriptor = require('generic-bitmask').Descriptor;
var mask = new Bitmask(),
descriptors = new Descriptor({
read: 1,
write: 2
});
descriptor.add('read', mask);
descriptor.validate('read', mask);
descriptor.extract(mask);
descriptor.remove('read', mask);
descriptor.defined('read')