You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
9
8
10
-
Examples
11
-
===
9
+
## Examples
10
+
11
+
###Basic usage
12
12
13
-
Basic usage
14
-
---
15
13
```javascript
16
14
let bs =newBitSet;
17
15
bs.set(128, 1); // Set bit at position 128
18
16
console.log(bs.toString(16)); // Print out a hex dump with one bit set
19
17
```
20
18
21
-
Flipping bits
22
-
---
19
+
###Flipping bits
20
+
23
21
```javascript
24
22
let bs =newBitSet;
25
23
bs
@@ -33,15 +31,15 @@ if (str === "111111111111111111111111111000000011111111111111111111111111111") {
33
31
}
34
32
```
35
33
36
-
Range Set
37
-
---
34
+
###Range Set
35
+
38
36
```javascript
39
37
let bs =newBitSet;
40
38
bs.setRange(10, 18, 1); // Set a 1 between 10 and 18, inclusive
41
39
```
42
40
43
-
User permissions
44
-
---
41
+
###User permissions
42
+
45
43
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:
The default `BitSet` constructor accepts a single value of one the following types :
96
94
97
95
- String
@@ -108,118 +106,118 @@ The default `BitSet` constructor accepts a single value of one the following typ
108
106
- A BitSet object, which get copied over
109
107
110
108
111
-
Functions
112
-
===
109
+
##Functions
110
+
113
111
114
112
The data type Mixed can be either a BitSet object, a String or an integer representing a native bitset with 31 bits.
115
113
116
114
117
-
BitSet set(ndx[, value=1])
118
-
---
115
+
###BitSet set(ndx[, value=1])
116
+
119
117
Mutable; Sets value 0 or 1 to index `ndx` of the bitset
120
118
121
119
int get(ndx)
122
120
---
123
121
Gets the value at index ndx
124
122
125
-
BitSet setRange(from, to[, value=1])
126
-
---
123
+
###BitSet setRange(from, to[, value=1])
124
+
127
125
Mutable; Helper function for set, to set an entire range to a given value
128
126
129
-
BitSet clear([from[, to]])
130
-
---
127
+
###BitSet clear([from[, to]])
128
+
131
129
Mutable; Sets a portion of a given bitset to zero
132
130
133
131
- If no param is given, the whole bitset gets cleared
134
132
- If one param is given, the bit at this index gets cleared
135
133
- If two params are given, the range is cleared
136
134
137
-
BitSet slice([from[, to]])
138
-
---
135
+
###BitSet slice([from[, to]])
136
+
139
137
Immutable; Extracts a portion of a given bitset as a new bitset
140
138
141
139
- If no param is given, the bitset is getting cloned
142
140
- If one param is given, the index is used as offset
143
141
- If two params are given, the range is returned as new BitSet
144
142
145
-
BitSet flip([from[, to]])
146
-
---
143
+
###BitSet flip([from[, to]])
144
+
147
145
Mutable; Toggles a portion of a given bitset
148
146
149
147
- If no param is given, the bitset is inverted
150
148
- If one param is given, the bit at the index is toggled
151
149
- If two params are given, the bits in the given range are toggled
152
150
153
-
BitSet not()
154
-
---
151
+
###BitSet not()
152
+
155
153
Immutable; Calculates the bitwise complement
156
154
157
-
BitSet and(Mixed x)
158
-
---
155
+
###BitSet and(Mixed x)
156
+
159
157
Immutable; Calculates the bitwise intersection of two bitsets
160
158
161
-
BitSet or(Mixed x)
162
-
---
159
+
###BitSet or(Mixed x)
160
+
163
161
Immutable; Calculates the bitwise union of two bitsets
164
162
165
-
BitSet xor(Mixed x)
166
-
---
163
+
###BitSet xor(Mixed x)
164
+
167
165
Immutable; Calculates the bitwise xor between two bitsets
168
166
169
-
BitSet andNot(Mixed x)
170
-
---
167
+
###BitSet andNot(Mixed x)
168
+
171
169
Immutable; Calculates the bitwise difference of two bitsets (this is not the nand operation!)
172
170
173
-
BitSet clone()
174
-
---
171
+
###BitSet clone()
172
+
175
173
Immutable; Clones the actual object
176
174
177
-
Array toArray()
178
-
---
175
+
###Array toArray()
176
+
179
177
Returns an array with all indexes set in the bitset
180
178
181
-
String toString([base=2])
182
-
---
179
+
###String toString([base=2])
180
+
183
181
Returns a string representation with respect to the base
184
182
185
-
int cardinality()
186
-
---
183
+
###int cardinality()
184
+
187
185
Calculates the number of bits set
188
186
189
-
int msb()
190
-
---
187
+
###int msb()
188
+
191
189
Calculates the most significant bit (the left most)
192
190
193
-
int ntz()
194
-
---
191
+
###int ntz()
192
+
195
193
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.
196
194
197
-
int lsb()
198
-
---
195
+
###int lsb()
196
+
199
197
Calculates the least significant bit (the right most)
200
198
201
-
bool isEmpty()
202
-
---
199
+
###bool isEmpty()
200
+
203
201
Checks if the bitset has all bits set to zero
204
202
205
-
bool equals()
206
-
---
203
+
###bool equals()
204
+
207
205
Checks if two bitsets are the same
208
206
209
-
BitSet.fromBinaryString(str)
210
-
---
207
+
###BitSet.fromBinaryString(str)
208
+
211
209
Alternative constructor to pass with a binary string
212
210
213
-
BitSet.fromHexString(str)
214
-
---
211
+
###BitSet.fromHexString(str)
212
+
215
213
Alternative constructor to pass a hex string
216
214
217
-
BitSet.Random([n=32])
218
-
---
215
+
###BitSet.Random([n=32])
216
+
219
217
Create a random BitSet with a maximum length of n bits
220
218
221
-
Iterator Interface
222
-
===
219
+
##Iterator Interface
220
+
223
221
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.
224
222
225
223
```js
@@ -231,28 +229,29 @@ for (let b of bs) {
231
229
232
230
Note: If the bitset is inverted so that all leading bits are 1, the iterator must be stopped by the user!
233
231
234
-
Coding Style
235
-
===
232
+
233
+
##Coding Style
234
+
236
235
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.
237
236
238
-
Build the library
239
-
===
240
-
Gulp is optional for minifying with Google Closure Compiler. After cloning the Git repository, do:
237
+
##Building the library
238
+
239
+
After cloning the Git repository run:
241
240
242
241
```
243
242
npm install
244
-
gulp
243
+
npm run build
245
244
```
246
245
247
-
Run a test
248
-
===
246
+
##Run a test
247
+
249
248
Testing the source against the shipped test suite is as easy as
0 commit comments