- Notifications
You must be signed in to change notification settings - Fork31
The RAW arbitrary size Bit-Vector implementation in JavaScript
License
rawify/BitSet.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
BitSet.js is an infiniteBit-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.
letbs=newBitSet;bs.set(128,1);// Set bit at position 128console.log(bs.toString(16));// Print out a hex dump with one bit set
letbs=newBitSet;bs.flip(0,62).flip(29,35);letstr=bs.toString();if(str==="111111111111111111111111111000000011111111111111111111111111111"){console.log("YES!");}
letbs=newBitSet;bs.setRange(10,18,1);// Set a 1 between 10 and 18, inclusive
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:
letP_READ=2;// Bit posletP_WRITE=1;letP_EXEC=0;letuser=newBitSet;user.set(P_READ);// Give read permsuser.set(P_WRITE);// Give write permsletgroup=newBitSet(P_READ);letworld=newBitSet(P_EXEC);console.log("0"+user.toString(8)+group.toString(8)+world.toString(8));
npm install bitset
<scriptsrc="bitset.js"></script><script>console.log(BitSet("111"));</script>
<scriptsrc="require.js"></script><script>requirejs(['bitset.js'],function(BitSet){console.log(BitSet("1111"));});</script>
The defaultBitSet
constructor accepts a single value of one the following types :
- String
- Binary strings :
new BitSet("010101")
- Binary strings with prefix :
new BitSet("0b010101")
- Hexadecimal strings with prefix
new BitSet("0xaffe")
- Binary strings :
- Array
- The values of the array are the indices to be set to 1 :
new BitSet([1,12,9])
- The values of the array are the indices to be set to 1 :
- Uint8Array
- A binary representation in 8 bit form
- Number
- A binary value
- BitSet
- A BitSet object, which get copied over
The data type Mixed can be either a BitSet object, a String or an integer representing a native bitset with 31 bits.
Mutable; Sets value 0 or 1 to indexndx
of the bitset
Gets the value at index ndx
Mutable; Helper function for set, to set an entire range to a given value
Mutable; Sets a portion of a given bitset to zero
- If no param is given, the whole bitset gets cleared
- If one param is given, the bit at this index gets cleared
- If two params are given, the range is cleared
Immutable; Extracts a portion of a given bitset as a new bitset
- If no param is given, the bitset is getting cloned
- If one param is given, the index is used as offset
- If two params are given, the range is returned as new BitSet
Mutable; Toggles a portion of a given bitset
- If no param is given, the bitset is inverted
- If one param is given, the bit at the index is toggled
- If two params are given, the bits in the given range are toggled
Immutable; Calculates the bitwise complement
Immutable; Calculates the bitwise intersection of two bitsets
Immutable; Calculates the bitwise union of two bitsets
Immutable; Calculates the bitwise xor between two bitsets
Immutable; Calculates the bitwise difference of two bitsets (this is not the nand operation!)
Immutable; Clones the actual object
Returns an array with all indexes set in the bitset
Returns a string representation with respect to the base
Calculates the number of bits set
Calculates the most significant bit (the left most)
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.
Calculates the least significant bit (the right most)
Checks if the bitset has all bits set to zero
Checks if two bitsets are the same
Alternative constructor to pass with a binary string
Alternative constructor to pass a hex string
Create a random BitSet with a maximum length of n bits
ABitSet
object is iterable. The iterator gets all bits up to the most significant bit. If no bits are set, the iteration stops immediately.
letbs=BitSet.Random(55);for(letbofbs){console.log(b);}
Note: If the bitset is inverted so that all leading bits are 1, the iterator must be stopped by the user!
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.
After cloning the Git repository run:
npm installnpm run build
Testing the source against the shipped test suite is as easy as
npm run test
Copyright (c) 2024,Robert EiseleLicensed under the MIT license.
About
The RAW arbitrary size Bit-Vector implementation in JavaScript
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors13
Uh oh!
There was an error while loading.Please reload this page.