// Using a single number to store the bits
class BitSet {
private bits: number;
constructor() {
this.bits = 0;
}
set(index: number): void {
this.bits |= (1 << index);
}
clear(index: number): void {
this.bits &= ~(1 << index);
}
get(index: number): boolean {
return (this.bits & (1 << index)) !== 0;
}
print(): void {
console.log(this.bits.toString(2).padStart(16, '0'));
}
}
const bitset = new BitSet();
bitset.set(4);
bitset.print();
console.log(bitset.get(4)); // true
bitset.clear(4);
console.log(bitset.get(4)); // false
bitset.print();
/*
run:
"0000000000010000"
true
false
"0000000000000000"
*/