https://github.com/binarymax/bitflags
Small library for working with an arbitrarily large array of booleans
https://github.com/binarymax/bitflags
Last synced: 6 months ago
JSON representation
Small library for working with an arbitrarily large array of booleans
- Host: GitHub
- URL: https://github.com/binarymax/bitflags
- Owner: binarymax
- License: mit
- Created: 2015-02-04T10:53:50.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-28T14:33:38.000Z (over 10 years ago)
- Last Synced: 2025-07-08T12:26:51.628Z (7 months ago)
- Language: JavaScript
- Size: 145 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bitflags
Small library for working with an arbitrarily large array of booleans
## Install
### npm
```
npm install bitflags
```
## API
```
var bitflags = require('bitflags');
var flags = bitflags(1000000); //Creates a million boolean flags
```
### get
Gets the bool at offset
```
flags.get(n) //returns true or false
```
### set
Sets a bit at offset to bool
```
flags.set(n,0) //sets to false
```
```
flags.set(n,1) //sets to true
```
```
flags.set(n,'truthy') //sets to true
```
### on
Sets the bit at offset to true
```
flags.on(n)
```
### off
Sets the bit at offset to false
```
flags.off(n)
```
### flip
Flip and returns the value of a single bit at offset
```
flags.flip(n)
```
### fill
Reset to all 1's
```
flags.fill()
```
### clear
Reset to all 0's
```
flags.clear()
```
### size
Return exact size in bits
*note: may be larger than size specified in constructor, due to storage in octets*
```
flags.size()
```
## Example
Sets a flag for each prime in the first 2000000 integers, using Sieve of Eratosthenes, returns a function to test for primes:
```javascript
var getSieveOfEratosthenes = function() {
var size = 2000000;
var bits = bitflags(size);
bits.fill();
bits.set(0,0);
var lim = Math.ceil(Math.sqrt(size));
for(var i=2;i<=lim;i++) {
if(bits.get(i)){
for(var j=i*i;j<=size;j+=i) {
bits.set(j,0);
}
}
};
return function(n){ return bits.get(n); };
};
```