Public
Edited
Sep 6, 2022
3 stars
Insert cell
Insert cell
Bits = class {
constructor(n = 4) {
this.values = new Uint8Array(n);
}
get(i) {
const byte = i >> 3;
const bit = i % 8;
return (this.values[byte] >> bit) & 1;
}
set(i, value) {
const byte = i >> 3;

// grow the underlying buffer as necessary
if (1 + byte > this.values.length) {
const buf = new Uint8Array(
Math.max(1 + byte, 2 * this.values.length)
);
buf.set(this.values);
this.values = buf;
}

const bit = i % 8;
if (value) this.values[byte] |= 1 << bit;
else this.values[byte] &= ~(1 << bit);

return this; // chain
}
}
Insert cell
A = new Bits()
Insert cell
A.set(1, 1).get(1)
Insert cell
A.set(32, 1).get(32)
Insert cell
A.set(32, 0).get(32)
Insert cell
A.set(33, 1).get(33)
Insert cell
A.get(12255)
Insert cell
A.values
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more