var buffer = new Uint8Array(1);
function readBit(buffer, i, bit){
return (buffer[i] >> bit) % 2;
}
function setBit(buffer, i, bit, value){
if(value == 0){
buffer[i] &= ~(1 << bit);
}else{
buffer[i] |= (1 << bit);
}
}
// write bit 0 of buffer[0]
setBit(buffer, 0, 0, 1)
// write bit 1 of buffer[0]
setBit(buffer, 0, 1, 1)
setBit(buffer, 0, 1, 0)
// write bit 2 of buffer[0]
setBit(buffer, 0, 2, 0)
// write bit 3 of buffer[0]
setBit(buffer, 0, 3, 0)
setBit(buffer, 0, 3, 1)
// read back the bits
console.log(
readBit(buffer, 0, 0),
readBit(buffer, 0, 1),
readBit(buffer, 0, 2),
readBit(buffer, 0, 3)
);