6 ms·
Ech, is that right? I don't understand `wordSizeShift`. I have always done bitset tests as follows: this.set = function(bit, value) { const index = Mat
by cjhanks 9y ago
Ech, is that right? I don't understand `wordSizeShift`.
I have always done bitset tests as follows:
this.set = function(bit, value) {
const index = Math.floor(bit / BitsPerElement);
const offset = bit - (index * BitsPerElement);
var element = this.buffer[index];
if (value)
element |= (1 << offset);
else
element &= ~(1 << offset);
this.buffer[index] = element;
}
this.get = function(bit) {
const index = Math.floor(bit / BitsPerElement);
const offset = bit - (index * BitsPerElement);
const element = this.buffer[index];
return (element & (1 << offset));
}
- 19eightyfour 9y agoI could have the code wrong or overly complex. Thank you for posting your code. I'll see what it works to include. That certainly looks tight / good, what you've got there! A reason I wrote it the way i did was I wanted to get rid of the conditional testing if value is zero or not. Also `wordSizeShift` is just the number of bits to right shift by to divide by the word size, to save a division.