-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitSet.js
More file actions
29 lines (23 loc) · 655 Bytes
/
BitSet.js
File metadata and controls
29 lines (23 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class BitSet {
constructor(size = 32) {
this.size = size;
this.bits = new Uint32Array(Math.ceil(size / 32));
}
add(value) {
const index = Math.floor(value / 32);
const position = value % 32;
this.bits[index] |= 1 << position;
}
has(value) {
const index = Math.floor(value / 32);
const position = value % 32;
return (this.bits[index] & (1 << position)) !== 0;
}
}
// Example usage:
const bitSet = new BitSet();
bitSet.add(10);
bitSet.add(32);
bitSet.add(50);
console.log(bitSet.has(10)); // Output: true
console.log(bitSet.has(15)); // Output: false