{"name":"bitset","version":"5.2.2","keywords":["bits","set","twiddle","bit set","bit array","bit vector","bit string","bitset","bitarray","bitvector","bitstring","binary","bitmap","bitwise"],"author":{"url":"https://raw.org/","name":"Robert Eisele","email":"robert@raw.org"},"license":"MIT","_id":"bitset@5.2.2","maintainers":[{"name":"infusion","email":"robert@xarg.org"}],"homepage":"https://raw.org/article/javascript-bit-array/","bugs":{"url":"https://github.com/rawify/BitSet.js/issues"},"dist":{"shasum":"f9602be20dc6ead867e3686daf23588ae54f3979","tarball":"http://registry-npm.ywwl.com/bitset/download/bitset-5.2.2.tgz","fileCount":9,"integrity":"sha512-J+LnskKHNJBWC14BPMN+d0XWAiBr9kKDhdiL0zi7D0QK9AKR7Pwx2q/B/UBWpJ0Z8SmdUbY1C2aOwnRtzInwzQ==","signatures":[{"sig":"MEUCIFtrfKb1jOzCnZQng7O6HqNA3wqpyagsKeOHFCK34ECRAiEApKr9S0kZh5BsefsNjt+/0JSumMKlik8+n93qvnpku9w=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":105119,"size":18950,"noattachment":false,"key":"/bitset/-/bitset-5.2.2.tgz"},"main":"./dist/bitset.js","title":"BitSet","types":"./bitset.d.ts","unpkg":"./dist/bitset.min.js","module":"./dist/bitset.mjs","browser":"./dist/bitset.min.js","engines":{"node":"*"},"exports":{".":{"types":"./bitset.d.ts","import":"./dist/bitset.mjs","require":"./dist/bitset.js"}},"funding":{"url":"https://github.com/sponsors/rawify","type":"github"},"gitHead":"c5043e1f771f351da81857fb0b335019624f0b20","private":false,"scripts":{"test":"mocha tests/*.js","build":"crude-build BitSet"},"_npmUser":{"name":"infusion","email":"robert@xarg.org"},"repository":{"url":"git+ssh://git@github.com/rawify/BitSet.js.git","type":"git"},"_npmVersion":"8.19.4","description":"A performance optimized infinite bit vector library","directories":{"test":"tests"},"_nodeVersion":"16.20.1","_hasShrinkwrap":false,"devDependencies":{"mocha":"*","crude-build":"^0.0.4"},"_npmOperationalInternal":{"tmp":"tmp/bitset_5.2.2_1728238880897_0.8841086593616514","host":"s3://npm-registry-packages"},"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 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.\n\n## Examples\n\n### Basic usage\n\n```javascript\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\n\n```\nnpm install bitset\n```\n\n## Using BitSet.js with the browser\n\n```html\n<script src=\"bitset.js\"></script>\n<script>\n    console.log(BitSet(\"111\"));\n</script>\n```\n\n## Using BitSet.js with require.js\n\n```html\n<script src=\"require.js\"></script>\n<script>\nrequirejs(['bitset.js'],\nfunction(BitSet) {\n    console.log(BitSet(\"1111\"));\n});\n</script>\n```\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\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## Coding Style\n\nAs 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.\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) 2024, [Robert Eisele](https://raw.org/)\nLicensed under the MIT license.\n","publish_time":1728238881136,"_cnpm_publish_time":1728238881136,"dist-tags":{"latest":"5.3.0"}}