-
Notifications
You must be signed in to change notification settings - Fork 296
/
Bitset.js
38 lines (30 loc) · 827 Bytes
/
Bitset.js
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
30
31
32
33
34
35
36
37
38
class Bitset {
constructor(size) {
this.size = size;
this.bits = Array(Math.ceil(size / 32)).fill(0);
}
set(bit) {
const index = Math.floor(bit / 32);
const shift = bit % 32;
this.bits[index] |= (1 << shift);
}
reset(bit) {
const index = Math.floor(bit / 32);
const shift = bit % 32;
this.bits[index] &= ~(1 << shift);
}
test(bit) {
const index = Math.floor(bit / 32);
const shift = bit % 32;
return (this.bits[index] & (1 << shift)) !== 0;
}
}
// Example Usage
const bitset = new Bitset(100);
bitset.set(5);
bitset.set(10);
bitset.set(15);
console.log(bitset.test(5)); // Output: true
console.log(bitset.test(7)); // Output: false
bitset.reset(10);
console.log(bitset.test(10)); // Output: false