Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitset

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitset - npm Package Compare versions

Comparing version 5.1.1 to 5.2.0

dist/bitset.js

42

package.json
{
"name": "bitset",
"title": "BitSet",
"version": "5.1.1",
"homepage": "http://www.xarg.org/2014/03/javascript-bit-array/",
"version": "5.2.0",
"homepage": "https://raw.org/article/javascript-bit-array/",
"bugs": "https://github.com/rawify/BitSet.js/issues",
"description": "A performance optimized infinite bit vector library",

@@ -23,23 +24,44 @@ "keywords": [

],
"author": "Robert Eisele <robert@xarg.org> (http://www.xarg.org/)",
"main": "bitset",
"private": false,
"main": "./dist/bitset.js",
"module": "./dist/bitset.mjs",
"types": "./bitset.d.ts",
"browser": "./dist/bitset.min.js",
"unpkg": "./dist/bitset.min.js",
"readmeFilename": "README.md",
"license": "MIT OR GPL-2.0",
"exports": {
".": {
"types": "./bitset.d.ts",
"require": "./dist/bitset.js",
"import": "./dist/bitset.mjs"
}
},
"repository": {
"type": "git",
"url": "http://github.com/infusion/bitset.js.git"
"url": "git@github.com:rawify/BitSet.js.git"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/rawify"
},
"author": {
"name": "Robert Eisele",
"email": "robert@raw.org",
"url": "https://raw.org/"
},
"license": "MIT",
"engines": {
"node": "*"
},
"directories": {
"test": "tests"
},
"scripts": {
"build": "crude-build BitSet",
"test": "mocha tests/*.js"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"google-closure-compiler": "*",
"gulp": "*"
"crude-build": "^0.0.1",
"mocha": "*"
}
}
# BitSet.js
[![NPM Package](https://nodei.co/npm-dl/bitset.png?months=6&height=1)](https://npmjs.org/package/bitset)
[![Build Status](https://travis-ci.org/infusion/BitSet.js.svg)](https://travis-ci.org/infusion/BitSet.js)
[![NPM Package](https://img.shields.io/npm/v/bitset.svg?style=flat)](https://npmjs.org/package/bitset "View this project on npm")
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
BitSet.js is an infinite [Bit-Array](http://en.wikipedia.org/wiki/Bit_array) (aka bit vector, bit string, bit set) implementation in JavaScript. That means that if you invert a bit vector, the leading ones get remembered. As far as I can tell, BitSet.js is the only library which has this feature. It is also heavily benchmarked against other implementations and is the most performant implementation to date.
Examples
===
BitSet.js is an infinite [Bit-Array](http://en.wikipedia.org/wiki/Bit_array) (aka bit vector, bit string, bit set) implementation in JavaScript. Infinite means that if you invert a bit vector, the leading ones get remembered. As far as I can tell, BitSet.js is the only library which has this feature. It is also heavily benchmarked against other implementations and is the most performant implementation to date.
Basic usage
---
## Examples
###Basic usage
```javascript

@@ -21,4 +19,4 @@ let bs = new BitSet;

Flipping bits
---
###Flipping bits
```javascript

@@ -37,4 +35,4 @@ let bs = new BitSet;

Range Set
---
###Range Set
```javascript

@@ -45,4 +43,4 @@ let bs = new BitSet;

User permissions
---
###User permissions
If you want to store user permissions in your database and use BitSet for the bit twiddling, you can start with the following Linux-style snippet:

@@ -64,5 +62,5 @@ ```javascript

Installation
===
##Installation
```

@@ -76,4 +74,4 @@ npm install bitset

Using BitSet.js with the browser
===
##Using BitSet.js with the browser
```html

@@ -86,4 +84,4 @@ <script src="bitset.js"></script>

Using BitSet.js with require.js
===
##Using BitSet.js with require.js
```html

@@ -99,4 +97,4 @@ <script src="require.js"></script>

Constructor
===
##Constructor
The default `BitSet` constructor accepts a single value of one the following types :

@@ -118,10 +116,10 @@

Functions
===
##Functions
The data type Mixed can be either a BitSet object, a String or an integer representing a native bitset with 31 bits.
BitSet set(ndx[, value=0])
---
###BitSet set(ndx[, value=1])
Mutable; Sets value 0 or 1 to index `ndx` of the bitset

@@ -133,8 +131,8 @@

BitSet setRange(from, to[, value=1])
---
###BitSet setRange(from, to[, value=1])
Mutable; Helper function for set, to set an entire range to a given value
BitSet clear([from[, to]])
---
###BitSet clear([from[, to]])
Mutable; Sets a portion of a given bitset to zero

@@ -146,4 +144,4 @@

BitSet slice([from[, to]])
---
###BitSet slice([from[, to]])
Immutable; Extracts a portion of a given bitset as a new bitset

@@ -155,4 +153,4 @@

BitSet flip([from[, to]])
---
###BitSet flip([from[, to]])
Mutable; Toggles a portion of a given bitset

@@ -164,72 +162,72 @@

BitSet not()
---
###BitSet not()
Immutable; Calculates the bitwise complement
BitSet and(Mixed x)
---
###BitSet and(Mixed x)
Immutable; Calculates the bitwise intersection of two bitsets
BitSet or(Mixed x)
---
###BitSet or(Mixed x)
Immutable; Calculates the bitwise union of two bitsets
BitSet xor(Mixed x)
---
###BitSet xor(Mixed x)
Immutable; Calculates the bitwise xor between two bitsets
BitSet andNot(Mixed x)
---
###BitSet andNot(Mixed x)
Immutable; Calculates the bitwise difference of two bitsets (this is not the nand operation!)
BitSet clone()
---
###BitSet clone()
Immutable; Clones the actual object
Array toArray()
---
###Array toArray()
Returns an array with all indexes set in the bitset
String toString([base=2])
---
###String toString([base=2])
Returns a string representation with respect to the base
int cardinality()
---
###int cardinality()
Calculates the number of bits set
int msb()
---
###int msb()
Calculates the most significant bit (the left most)
int ntz()
---
###int ntz()
Calculates the number of trailing zeros (zeros on the right). If all digits are zero, `Infinity` is returned, since BitSet.js is an arbitrary large bit vector implementation.
int lsb()
---
###int lsb()
Calculates the least significant bit (the right most)
bool isEmpty()
---
###bool isEmpty()
Checks if the bitset has all bits set to zero
bool equals()
---
###bool equals()
Checks if two bitsets are the same
BitSet.fromBinaryString(str)
---
###BitSet.fromBinaryString(str)
Alternative constructor to pass with a binary string
BitSet.fromHexString(str)
---
###BitSet.fromHexString(str)
Alternative constructor to pass a hex string
BitSet.Random([n=32])
---
###BitSet.Random([n=32])
Create a random BitSet with a maximum length of n bits
Iterator Interface
===
##Iterator Interface
A `BitSet` object is iterable. The iterator gets all bits up to the most significant bit. If no bits are set, the iteration stops immediately.

@@ -246,26 +244,27 @@

Coding Style
===
##Coding Style
As every library I publish, BitSet.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
Build the library
===
Gulp is optional for minifying with Google Closure Compiler. After cloning the Git repository, do:
##Building the library
After cloning the Git repository run:
```
npm install
gulp
npm run build
```
Run a test
===
##Run a test
Testing the source against the shipped test suite is as easy as
```
npm test
npm run test
```
Copyright and licensing
===
Copyright (c) 2014-2018, [Robert Eisele](https://www.xarg.org/)
Dual licensed under the MIT or GPL Version 2 licenses.
## Copyright and licensing
Copyright (c) 2025, [Robert Eisele](https://raw.org/)
Licensed under the MIT license.
var assert = require('assert');
var should = require('should');
const BitSet = require('bitset');
const assert = require('assert');
var BitSet = require('../bitset.min');
describe('BitSet', function () {
describe('BitSet', function() {
it('should Construct', function () {
it('should Construct', function() {
var bs = new BitSet(19219);

@@ -21,3 +19,3 @@ assert.equal(bs.toString(10), '19219');

it('should set an individual bit', function() {
it('should set an individual bit', function () {
var bs = new BitSet();

@@ -28,3 +26,3 @@ bs.set(31);

it('should find first set', function() {
it('should find first set', function () {
var bs = new BitSet();

@@ -35,3 +33,3 @@ bs.set(31);

it('should not be able to find first set in an empty bitset', function() {
it('should not be able to find first set in an empty bitset', function () {
var bs = new BitSet();

@@ -41,3 +39,3 @@ assert.equal(bs.msb(), Infinity);

it('should unset a bit', function() {
it('should unset a bit', function () {
var bs = new BitSet();

@@ -49,3 +47,3 @@ bs.set(31);

it('should Hex in - Hex out', function() {
it('should Hex in - Hex out', function () {

@@ -65,3 +63,3 @@ var bs = new BitSet("0xD00000005");

it('should Get range', function() {
it('should Get range', function () {

@@ -78,3 +76,3 @@ var bs = new BitSet("0xff900018283821283");

it('should check empty', function() {
it('should check empty', function () {

@@ -91,3 +89,3 @@ var bs = new BitSet();

it('should flip/clear 2', function() {
it('should flip/clear 2', function () {

@@ -104,3 +102,3 @@ var bs = new BitSet;

it('should flip/clear 3', function() {
it('should flip/clear 3', function () {

@@ -119,8 +117,8 @@ var bs = new BitSet;

it('should flip/clear 4', function() {
it('should flip/clear 4', function () {
var bs = new BitSet;
bs
.flip(0, 62)
.flip(29, 35);
.flip(0, 62)
.flip(29, 35);

@@ -131,3 +129,3 @@ assert.equal(bs.toString(), "111111111111111111111111111000000011111111111111111111111111111");

it('should eat own dogfood', function() {
it('should eat own dogfood', function () {

@@ -140,3 +138,3 @@ var bs = new BitSet(4);

it('should flip range check', function() {
it('should flip range check', function () {

@@ -149,3 +147,3 @@ var bs = new BitSet;

it('should And', function() {
it('should And', function () {

@@ -157,3 +155,3 @@ var bs = new BitSet("0xff05");

it('should Or', function() {
it('should Or', function () {

@@ -164,3 +162,3 @@ var bs = new BitSet(256);

it('should Xor', function() {
it('should Xor', function () {

@@ -171,3 +169,3 @@ var bs = new BitSet("1010");

it('should Equals', function() {
it('should Equals', function () {

@@ -181,3 +179,3 @@ var bs = new BitSet('100000000000111010101');

it('should Cardinality', function() {
it('should Cardinality', function () {

@@ -188,3 +186,3 @@ var bs = new BitSet('1000000000000000000001101');

it('should msbit', function() {
it('should msbit', function () {

@@ -195,3 +193,3 @@ var bs = new BitSet('1000000000000000000001101');

it('should msbit set', function() {
it('should msbit set', function () {

@@ -201,4 +199,4 @@ var bs = new BitSet;

bs = bs
.set(100)
.set(333);
.set(100)
.set(333);

@@ -208,3 +206,3 @@ assert.equal(bs.msb(), 333);

it('should slice negated', function() {
it('should slice negated', function () {

@@ -222,3 +220,3 @@ var bs = new BitSet(4);

it('should setRange 2', function() {
it('should setRange 2', function () {

@@ -233,3 +231,3 @@ var bs = new BitSet;

it('should setRange 3', function() {
it('should setRange 3', function () {

@@ -243,3 +241,3 @@ var bs = new BitSet;

it('should setRange 1', function() {
it('should setRange 1', function () {

@@ -253,3 +251,3 @@ var bs = new BitSet;

it('should msbit should work negative numbers', function() {
it('should msbit should work negative numbers', function () {
var flipped = new BitSet().not();

@@ -260,3 +258,3 @@

it('should ntzit', function() {
it('should ntzit', function () {

@@ -272,3 +270,3 @@ assert.equal(new BitSet('10000000000110001000000000').ntz(), 9);

it('should set', function() {
it('should set', function () {

@@ -278,6 +276,6 @@ var bs = new BitSet;

bs
.set(4, 1) // Set bit on 4th pos
.set(0) // Set bit on 0th pos
.set(22, 1)
.set(33, 1); // Set
.set(4, 1) // Set bit on 4th pos
.set(0) // Set bit on 0th pos
.set(22, 1)
.set(33, 1); // Set

@@ -296,26 +294,26 @@ assert.equal(bs.toString(), '1000000000010000000000000000010001');

it('should lsbit', function() {
it('should lsbit', function () {
assert.equal(
new BitSet('10000000000110001000000000').lsb(),
9
);
new BitSet('10000000000110001000000000').lsb(),
9
);
assert.equal(
new BitSet('00000000000000000000000000').lsb(),
0
);
new BitSet('00000000000000000000000000').lsb(),
0
);
assert.equal(
new BitSet('10000000000000000000000000').lsb(),
25
);
new BitSet('10000000000000000000000000').lsb(),
25
);
assert.equal(
new BitSet('10000000100000000000000000000000000000000000').lsb(),
35
);
new BitSet('10000000100000000000000000000000000000000000').lsb(),
35
);
});
it('should string and', function() {
it('should string and', function () {

@@ -330,22 +328,22 @@ var bsa = new BitSet();

bsa.cardinality().should.equal(0);
bsb.cardinality().should.equal(1);
assert.equal(bsa.cardinality(), 0);
assert.equal(bsb.cardinality(), 1);
bsa.get(0).should.equal(0);
bsb.get(0).should.equal(0);
assert.equal(bsa.get(0), 0);
assert.equal(bsb.get(0), 0);
bsa.get(32).should.equal(0);
bsb.get(32).should.equal(1);
assert.equal(bsa.get(32), 0);
assert.equal(bsb.get(32), 1);
for (var i = 0; i < 64; i++) {
bsa.get(i).should.equal(0);
bsb.get(i).should.equal(i === 32 ? 1 : 0);
assert.equal(bsa.get(i), 0);
assert.equal(bsb.get(i), i === 32 ? 1 : 0);
}
bsa.toString().should.equal('0');
bsb.toString().should.equal('100000000000000000000000000000000');
assert.equal(bsa.toString(), '0');
assert.equal(bsb.toString(), '100000000000000000000000000000000');
});
it('should string or', function() {
it('should string or', function () {

@@ -360,22 +358,22 @@ var bsa = new BitSet();

bsa.cardinality().should.equal(2);
bsb.cardinality().should.equal(1);
assert.equal(bsa.cardinality(), 2);
assert.equal(bsb.cardinality(), 1);
bsa.get(0).should.equal(1);
bsb.get(0).should.equal(0);
assert.equal(bsa.get(0), 1);
assert.equal(bsb.get(0), 0);
bsa.get(32).should.equal(1);
bsb.get(32).should.equal(1);
assert.equal(bsa.get(32), 1);
assert.equal(bsb.get(32), 1);
for (var i = 0; i < 64; i++) {
bsa.get(i).should.equal(i === 0 || i === 32 ? 1 : 0);
bsb.get(i).should.equal(i === 32 ? 1 : 0);
assert.equal(bsa.get(i), i === 0 || i === 32 ? 1 : 0);
assert.equal(bsb.get(i), i === 32 ? 1 : 0);
}
bsa.toString().should.eql('100000000000000000000000000000001');
bsb.toString().should.eql('100000000000000000000000000000000');
assert.equal(bsa.toString(), '100000000000000000000000000000001');
assert.equal(bsb.toString(), '100000000000000000000000000000000');
});
it('should pass setting in time', function(done) {
it('should pass setting in time', function (done) {

@@ -387,3 +385,3 @@ var bs = new BitSet;

}
(Date.now() - start).should.be.below(50);
assert((Date.now() - start) < 50);
done();

@@ -393,3 +391,3 @@

it('should string set', function() {
it('should string set', function () {

@@ -401,3 +399,3 @@ var bs = new BitSet('1000000000010000000000000000010001');

it('should set auto scale', function() {
it('should set auto scale', function () {

@@ -413,3 +411,3 @@ var bs = new BitSet;

it('should get', function() {
it('should get', function () {

@@ -424,3 +422,3 @@ var bs = new BitSet();

it('should work with flipped bitset: and', function() {
it('should work with flipped bitset: and', function () {

@@ -440,3 +438,3 @@ var a = new BitSet(0);

it('should work with different length scales: and', function() {
it('should work with different length scales: and', function () {

@@ -451,3 +449,3 @@ var a = new BitSet();

it('should work with different length scales: or', function() {
it('should work with different length scales: or', function () {

@@ -462,3 +460,3 @@ var a = new BitSet();

it('should work with different length scales: xor', function() {
it('should work with different length scales: xor', function () {

@@ -473,3 +471,3 @@ var a = new BitSet();

it('should work with zero: xor', function() {
it('should work with zero: xor', function () {

@@ -484,3 +482,3 @@ var a = new BitSet();

it('should work with inverted zero', function() {
it('should work with inverted zero', function () {

@@ -493,3 +491,3 @@ var a = new BitSet();

it('should cut off infinity', function() {
it('should cut off infinity', function () {

@@ -505,3 +503,3 @@ var a = new BitSet();

it('should and fuzz 00', function() {
it('should and fuzz 00', function () {

@@ -520,3 +518,3 @@ var a = new BitSet;

it('should and fuzz 01', function() {
it('should and fuzz 01', function () {

@@ -535,3 +533,3 @@ var a = new BitSet;

it('should and fuzz 10', function() {
it('should and fuzz 10', function () {

@@ -550,3 +548,3 @@ var a = new BitSet;

it('should and fuzz 11', function() {
it('should and fuzz 11', function () {

@@ -565,3 +563,3 @@ var a = new BitSet;

it('should or fuzz 00', function() {
it('should or fuzz 00', function () {

@@ -580,3 +578,3 @@ var a = new BitSet;

it('should or fuzz 01', function() {
it('should or fuzz 01', function () {

@@ -595,3 +593,3 @@ var a = new BitSet;

it('should or fuzz 10', function() {
it('should or fuzz 10', function () {

@@ -610,3 +608,3 @@ var a = new BitSet;

it('should or fuzz 11', function() {
it('should or fuzz 11', function () {

@@ -625,3 +623,3 @@ var a = new BitSet;

it('should xor fuzz 00', function() {
it('should xor fuzz 00', function () {

@@ -640,3 +638,3 @@ var a = new BitSet;

it('should xor fuzz 01', function() {
it('should xor fuzz 01', function () {

@@ -655,3 +653,3 @@ var a = new BitSet;

it('should xor fuzz 10', function() {
it('should xor fuzz 10', function () {

@@ -670,3 +668,3 @@ var a = new BitSet;

it('should xor fuzz 11', function() {
it('should xor fuzz 11', function () {

@@ -685,3 +683,3 @@ var a = new BitSet;

it('should andNot fuzz 00', function() {
it('should andNot fuzz 00', function () {

@@ -700,3 +698,3 @@ var a = new BitSet;

it('should andNot fuzz 01', function() {
it('should andNot fuzz 01', function () {

@@ -715,3 +713,3 @@ var a = new BitSet;

it('should andNot fuzz 10', function() {
it('should andNot fuzz 10', function () {

@@ -730,3 +728,3 @@ var a = new BitSet;

it('should andNot fuzz 11', function() {
it('should andNot fuzz 11', function () {

@@ -747,3 +745,3 @@ var a = new BitSet;

it('should work with simple xor use case', function() {
it('should work with simple xor use case', function () {
var bsa = new BitSet

@@ -758,10 +756,10 @@ bsa.set(0);

bsa.cardinality().should.eql(2);
bsa.get(0).should.eql(1);
bsa.get(32).should.eql(1);
assert.equal(bsa.cardinality(), 2);
assert.equal(bsa.get(0), 1);
assert.equal(bsa.get(32), 1);
bsa.toArray().toString().should.eql("0,32");
assert.equal(bsa.toArray().toString(), "0,32");
});
it('should get large undefined offsets', function() {
it('should get large undefined offsets', function () {

@@ -775,3 +773,3 @@ var a = new BitSet();

it('should work with empty checks', function() {
it('should work with empty checks', function () {

@@ -786,3 +784,3 @@ var a = new BitSet();

it('should work with inverting', function() {
it('should work with inverting', function () {

@@ -802,3 +800,3 @@ var x = new BitSet;

it('get higher bits correctly', function() {
it('get higher bits correctly', function () {

@@ -811,3 +809,3 @@ var a = new BitSet([34]).not();

it('should withstand some fuzz', function() {
it('should withstand some fuzz', function () {

@@ -837,3 +835,3 @@ for (var i = 0; i < 100; i++) {

it('should work with infinity strings', function() {
it('should work with infinity strings', function () {

@@ -845,3 +843,3 @@ var x = BitSet('11').not().set(35, 0);

it('should work with reported bug', function() {
it('should work with reported bug', function () {
var bs1 = new BitSet("101");

@@ -868,3 +866,3 @@ var ss = bs1.toString(); // expecting "101", actual "101" Note: bs1 = {length=1, [0]=5}

it('should clear', function() {
it('should clear', function () {
var bs1 = new BitSet("111101");

@@ -877,3 +875,3 @@ bs1.clear(5);

it('should iterate', function() {
it('should iterate', function () {

@@ -886,3 +884,3 @@ var val = "1111010101111010101010101111110010101010111110101001101010101011101010101111100011111100101011";

for (var b of bs) {
str+= b;
str += b;
}

@@ -892,3 +890,3 @@ assert.equal(str, val.split("").reverse().join(""));

it('should iterate zero', function() {
it('should iterate zero', function () {

@@ -901,3 +899,3 @@ var val = "0";

for (var b of bs) {
str+= b;
str += b;
}

@@ -907,3 +905,3 @@ assert.equal(str, "");

it('should iterate one', function() {
it('should iterate one', function () {

@@ -916,3 +914,3 @@ var val = "1";

for (var b of bs) {
str+= b;
str += b;
}

@@ -919,0 +917,0 @@ assert.equal(str, val);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc