I know you can't really manipulate the ArrayBuffer, but perhaps using a DataView or something like that, you could solve the problem of how to build a binary string one bit at a time. My goal is to build an encoding binary string one bit at a time, how do I do that in JavaScript? Everything I've seen only goes as far down as a Uint8Array
using bytes, but I want bits.
Asked
Active
Viewed 679 times
1

Lance
- 75,200
- 93
- 289
- 503
-
_"...but perhaps using a DataView"_ - Like in one of your old questions: [How to use ArrayBuffers with DataViews in JavaScript](https://stackoverflow.com/questions/51452398/how-to-use-arraybuffers-with-dataviews-in-javascript) ? – Andreas Jun 27 '20 at 09:41
1 Answers
2
Error checking of any nature is left as an exercise for the reader.
"use strict";
window.addEventListener('DOMContentLoaded', DOMContentLoaded, false);
function DOMContentLoaded(evt)
{
let result = new bitBuffer(8);
let _255 = [1,1,1,1,1,1,1,1];
let _128 = [1,0,0,0,0,0,0,0];
let _001 = [0,0,0,0,0,0,0,1];
// make 3 entries
for (var i=0; i<8; i++)
result.pushBit(_255[i]);
for (var i=0; i<8; i++)
result.pushBit(_128[i]);
for (var i=0; i<8; i++)
result.pushBit(_001[i]);
console.log( Array.from( result.getValue() ) );
}
class bitBuffer
{
constructor(numBytes)
{
this.buffer = new ArrayBuffer(numBytes);
this.view = new DataView( this.buffer );
this.numBitsDone = 0;
this.numBytesDone = 0;
}
pushBit(bitIsSet=false)
{
let curByte = this.view.getUint8( this.numBytesDone );
curByte <<= 1;
if (bitIsSet)
curByte |= 0x01;
this.view.setUint8( this.numBytesDone, curByte );
this.numBitsDone++;
if (this.numBitsDone == 8)
{
this.numBitsDone = 0;
this.numBytesDone++;
}
}
getValue()
{
return new Uint8Array(this.view.buffer);
}
}

enhzflep
- 12,927
- 2
- 32
- 51
-
Does this actually work? I don't think this is how you can use the arraybuffer :/ Because when I do `new DataView(buffer)` it returns all `0`'s. – Lance Jun 26 '20 at 04:31
-
"You cannot directly manipulate the contents of an ArrayBuffer;" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer#:~:text=You%20cannot%20directly%20manipulate%20the,the%20contents%20of%20the%20buffer. – Lance Jun 26 '20 at 04:32
-
@LancePollard - sure thing, done. You could use Uint16 (or 32 or 64) as the access mechanism, but then you start to run into issues surrounding endianness. – enhzflep Jun 27 '20 at 00:44
-
@LancePollard - sorry, I thought I had updated the code. It now creates and uses a DataView – enhzflep Jun 28 '20 at 15:22