Skip to content

Commit 5922942

Browse files
authored
Create BitSet.js
1 parent 55c8337 commit 5922942

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

03-DataStructures/BitSet.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class BitSet {
2+
constructor(size = 32) {
3+
this.size = size;
4+
this.bits = new Uint32Array(Math.ceil(size / 32));
5+
}
6+
7+
add(value) {
8+
const index = Math.floor(value / 32);
9+
const position = value % 32;
10+
this.bits[index] |= 1 << position;
11+
}
12+
13+
has(value) {
14+
const index = Math.floor(value / 32);
15+
const position = value % 32;
16+
return (this.bits[index] & (1 << position)) !== 0;
17+
}
18+
}
19+
20+
// Example usage:
21+
const bitSet = new BitSet();
22+
23+
bitSet.add(10);
24+
bitSet.add(32);
25+
bitSet.add(50);
26+
27+
console.log(bitSet.has(10)); // Output: true
28+
console.log(bitSet.has(15)); // Output: false
29+

0 commit comments

Comments
 (0)