{"name":"bitset","title":"BitSet","version":"5.3.0","type":"commonjs","description":"Fast arbitrary-length and infinite-complement bit sets with ranges, iteration, and bitwise operations","homepage":"https://raw.org/article/javascript-bit-array/","bugs":{"url":"https://github.com/rawify/BitSet.js/issues"},"keywords":["bits","set","twiddle","bit-set","bit-array","bit-vector","bit-string","bitset","bitarray","bitvector","bitstring","binary","bitmap","bitwise"],"private":false,"main":"./dist/bitset.js","module":"./dist/bitset.mjs","browser":"./dist/bitset.min.js","unpkg":"./dist/bitset.min.js","jsdelivr":"./dist/bitset.min.js","types":"./dist/bitset.d.ts","exports":{".":{"import":{"types":"./dist/bitset.d.mts","default":"./dist/bitset.mjs"},"require":{"types":"./dist/bitset.d.ts","default":"./dist/bitset.js"},"default":{"types":"./dist/bitset.d.ts","default":"./dist/bitset.js"}},"./browser":{"import":{"types":"./dist/bitset.d.mts","default":"./dist/bitset.min.mjs"},"default":"./dist/bitset.min.js"},"./package.json":"./package.json"},"sideEffects":false,"repository":{"type":"git","url":"git+ssh://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","directories":{"test":"tests"},"scripts":{"clean":"node scripts/clean.mjs","typecheck":"tsc --noEmit","build:types":"tsc -p tsconfig.build.json","build:js":"node scripts/build.mjs","build":"npm run clean && npm run build:types && npm run build:js","benchmark":"node benchmark/benchmark.js","test:types":"tsc -p tests/types/tsconfig.json","test:runtime":"node --test tests/*.test.js","test":"npm run build && npm run test:types && npm run test:runtime","prepublishOnly":"npm test"},"devDependencies":{"esbuild":"^0.25.9","fastbitset":"^0.5.2","typescript":"^5.9.2"},"gitHead":"dba225971dd901bfddc1c148683ba50bab520149","_id":"bitset@5.3.0","_nodeVersion":"26.8.1","_npmVersion":"11.19.0","dist":{"integrity":"sha512-0w53SRCSpU+cnePvl8Fj9WIRDrtX0+dE/UT97oBWLCOwNlQ3f/oKOk8DD+fYH8qBP5niV0Zhb94N1H1AgUJSFg==","shasum":"8fb3ef9493479e6c691fcdd2b525af8707898569","tarball":"http://registry-npm.ywwl.com/bitset/download/bitset-5.3.0.tgz","fileCount":11,"unpackedSize":168452,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQD5YVh8qXK4dsbbi/6hoDn1sel5hcwR2+WPwNxOZgjRagIhANmt4ornBARZBP6Ilpd9xxZ3BTVDFujKgDQ7Mfvdl8wq"}],"size":44069,"noattachment":false,"key":"/bitset/-/bitset-5.3.0.tgz"},"_npmUser":{"name":"infusion","email":"robert@raw.org"},"maintainers":[{"name":"infusion","email":"robert@raw.org"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/bitset_5.3.0_1788943820148_0.868244841627055"},"_hasShrinkwrap":false,"readme":"# BitSet.js\n\n[![NPM Package](https://img.shields.io/npm/v/bitset.svg?style=flat)](https://npmjs.org/package/bitset \"View this project on npm\")\n[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n\n\nBitSet.js is published as [`bitset`](https://www.npmjs.com/package/bitset). It implements arbitrary-length [bit arrays](http://en.wikipedia.org/wiki/Bit_array) and can also represent an infinite leading run of ones after complementing a value.\n\nUse it for compact integer-indexed sets, masks wider than JavaScript's 32-bit bitwise operators, and immutable bitwise combinations. Use `Set<number>` when members are sparse, non-integer, or routinely enumerated, and use `BigInt` when a single finite integer plus native arithmetic is the primary representation.\n\n## Examples\n\n### Basic usage\n\n```javascript\nimport BitSet from 'bitset';\n\nlet bs = new BitSet;\nbs.set(128, 1); // Set bit at position 128\nconsole.log(bs.toString(16)); // Print out a hex dump with one bit set\n```\n\n### Flipping bits\n\n```javascript\nlet bs = new BitSet;\nbs\n  .flip(0, 62)\n  .flip(29, 35);\n\nlet str = bs.toString();\n\nif (str === \"111111111111111111111111111000000011111111111111111111111111111\") {\n   console.log(\"YES!\");\n}\n```\n\n### Range Set\n\n```javascript\nlet bs = new BitSet;\nbs.setRange(10, 18, 1); // Set a 1 between 10 and 18, inclusive\n```\n\n### User permissions\n\nIf 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:\n```javascript\nlet P_READ  = 2; // Bit pos\nlet P_WRITE = 1;\nlet P_EXEC  = 0;\n\nlet user = new BitSet;\nuser.set(P_READ); // Give read perms\nuser.set(P_WRITE); // Give write perms\n\nlet group = new BitSet(P_READ);\nlet world = new BitSet(P_EXEC);\n\nconsole.log(\"0\" + user.toString(8) + group.toString(8) + world.toString(8));\n```\n\n## Installation\n\nYou can install `BitSet.js` via npm:\n\n```bash\nnpm install bitset\n```\n\nOr with yarn:\n\n```bash\nyarn add bitset\n```\n\nAlternatively, download or clone the repository:\n\n```bash\ngit clone https://github.com/rawify/BitSet.js\n```\n\n## Usage\n\nInclude the `bitset.min.js` file in your project:\n\n```html\n<script src=\"path/to/bitset.min.js\"></script>\n<script>\n  console.log(BitSet(\"1111\"));\n</script>\n```\n\nOr in a Node.js project:\n\n```javascript\nconst BitSet = require('bitset');\n```\n\nor\n\n```javascript\nimport BitSet from 'bitset';\n```\n\nVersion 5.3.0 is implemented in strict TypeScript and has no runtime dependencies. It ships dedicated CommonJS, ES module, browser-global, browser-ESM, and generated TypeScript declaration entry points without declaring a minimum Node.js version.\n\n| Environment | Entry point |\n| --- | --- |\n| CommonJS | `require('bitset')` |\n| ES module | `import BitSet from 'bitset'` |\n| Browser global | `dist/bitset.min.js` exposes `BitSet` |\n| Browser ES module | `dist/bitset.min.mjs` |\n| TypeScript | Generated declarations in `dist/` |\n\n## Constructor\n\nThe default `BitSet` constructor accepts a single value of one the following types :\n\n- String\n  - Binary strings : `new BitSet(\"010101\")`\n  - Binary strings with prefix : `new BitSet(\"0b010101\")`\n  - Hexadecimal strings with prefix `new BitSet(\"0xaffe\")`\n- Array\n  - The values of the array are the indices to be set to 1 : `new BitSet([1,12,9])`\n- [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)\n  - A binary representation in 8 bit form\n- Number\n  - A binary value\n- BitSet\n  - A BitSet object, which get copied over\n\n\n## Functions\n\n\nThe data type Mixed can be either a BitSet object, a String or an integer representing a native bitset with 31 bits.\n\nBit positions are zero-based, non-negative integer indices. `set()`, `setRange()`, `clear()`, and `flip()` mutate and return the receiver; bitwise operations such as `and()`, `or()`, `xor()`, `andNot()`, and `not()` return new instances.\n\nAn unbounded complement has infinitely many set bits: `cardinality()` and `msb()` return `Infinity`, while `toArray()` appends `Infinity` as a sentinel. Its bit-by-bit iterator is endless and therefore requires an explicit stopping condition. Use `slice(from, to)` first when a finite window is required.\n\n## Performance\n\nThe implementation keeps packed 32-bit words in monomorphic JavaScript arrays, uses four-word population counts, unrolled bitwise loops, low-bit extraction for enumeration, and direct range masks. Immutable operations allocate only their result and never mutate an operand.\n\nRun the reproducible comparison against the latest `fastbitset` release from npm with:\n\n```bash\nnpm run benchmark\n```\n\nThe benchmark covers construction, single-bit access, population count, enumeration, cloning, and immutable AND, OR, XOR, and AND-NOT operations. Results vary by JavaScript engine and hardware, so each operation is reported separately together with a geometric overall ratio.\n\n\n### BitSet set(ndx[, value=1])\n\nMutable; Sets value 0 or 1 to index `ndx` of the bitset\n\nint get(ndx)\n---\nGets the value at index ndx\n\n### BitSet setRange(from, to[, value=1])\n\nMutable; Helper function for set, to set an entire range to a given value\n\n### BitSet clear([from[, to]])\n\nMutable; Sets a portion of a given bitset to zero\n\n- If no param is given, the whole bitset gets cleared\n- If one param is given, the bit at this index gets cleared\n- If two params are given, the range is cleared\n\n### BitSet slice([from[, to]])\n\nImmutable; Extracts a portion of a given bitset as a new bitset\n\n- If no param is given, the bitset is getting cloned\n- If one param is given, the index is used as offset\n- If two params are given, the range is returned as new BitSet\n\n### BitSet flip([from[, to]])\n\nMutable; Toggles a portion of a given bitset\n\n- If no param is given, the bitset is inverted\n- If one param is given, the bit at the index is toggled\n- If two params are given, the bits in the given range are toggled\n\n### BitSet not()\n\nImmutable; Calculates the bitwise complement\n\n### BitSet and(Mixed x)\n\nImmutable; Calculates the bitwise intersection of two bitsets\n\n### BitSet or(Mixed x)\n\nImmutable; Calculates the bitwise union of two bitsets\n\n### BitSet xor(Mixed x)\n\nImmutable; Calculates the bitwise xor between two bitsets\n\n### BitSet andNot(Mixed x)\n\nImmutable; Calculates the bitwise difference of two bitsets (this is not the nand operation!)\n\n### BitSet clone()\n\nImmutable; Clones the actual object\n\n### Array toArray()\n\nReturns an array with all indexes set in the bitset\n\n### String toString([base=2])\n\nReturns a string representation with respect to the base\n\n### int cardinality()\n\nCalculates the number of bits set\n\n### int msb()\n\nCalculates the most significant bit (the left most)\n\n### int ntz()\n\nCalculates 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.\n\n### int lsb()\n\nCalculates the least significant bit (the right most)\n\n### bool isEmpty()\n\nChecks if the bitset has all bits set to zero\n\n### bool equals()\n\nChecks if two bitsets are the same\n\n### BitSet.fromBinaryString(str)\n\nAlternative constructor to pass with a binary string\n\n### BitSet.fromHexString(str)\n\nAlternative constructor to pass a hex string\n\n### BitSet.Random([n=32])\n\nCreate a random BitSet with a maximum length of n bits\n\n## Iterator Interface\n\nA `BitSet` object is iterable. The iterator gets all bits up to the most significant bit. If no bits are set, the iteration stops immediately.\n\n```js\nlet bs = BitSet.Random(55);\nfor (let b of bs) {\n  console.log(b);\n} \n```\n\nNote: If the bitset is inverted so that all leading bits are 1, the iterator must be stopped by the user!\n\n\n## Building the library\n\nAfter cloning the Git repository run:\n\n```\nnpm install\nnpm run build\n```\n\n## Run a test\n\nTesting the source against the shipped test suite is as easy as\n\n```\nnpm run test\n```\n\n## Copyright and Licensing\n\nCopyright (c) 2026, [Robert Eisele](https://raw.org/)\nLicensed under the MIT license.\n","publish_time":1788943820304,"_cnpm_publish_time":1788943820304,"dist-tags":{"latest":"5.3.0"}}