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 = Mask; // create a new instance of bitmaskvar mask = ; // Set the mask flags for the maskmask; // sets 3rd bit to positivemask; // sets 5th bit to positive // check which bit is set to trueconsole; // logs true;console; // logs false; // remove a bit that has been set as truemask;console; // logs false; // Get the raw state of the mask in decimal valueconsole; // logs 8; // Set the raw state of the mask. Useful to load saved mask statemask; // sets 3 and 5 to true;console; // logs true;console; // logs false;console; // logs true;console; // logs true;
Using the mask descriptor
var Bitmask = Mask Descriptor = Descriptor; // create a new instance of bitmaskvar mask = descriptors = read: 1 write: 2 ; // Set the mask flags for the maskdescriptor;descriptor; // returns truedescriptor; // returns ['read']descriptor; // Check if bit names are setdescriptor // returns true