You are not recognized as the original poster of this topic.
function printU8binary(bytes){
let len=bytes.length,
iPad=(""+(len-1)).length, //Math.max(1,Math.floor(Math.log10(len-1))+1),
array=Array.from(bytes);
console.log(array.map(x=>(x&0xFF).toString(16).toUpperCase().padStart(2,"0")).join(" "));
console.log(array.map((x,i)=>{
let byte=x&0xFF;
return(""+i).padStart(iPad)+": "+ //index
byte.toString(2).padStart(8)+ //binary
` (0x${byte.toString(16).padStart(2,"0")}; ${byte}; ${String.fromCharCode(byte)})`;
}).join("\n"));
console.log(array.map(x=>{ //visual representation
let o="";
for(let i=0;i<8;i++)o+=(x>>i)&1?"■":"□";
return o;
}).join("\n"));
console.log(String.fromCharCode(...array));
}
function randomU8array(len){
let array=new Uint8Array(len);
for(let i=0;i<array.length;i++)
array[i]=Math.floor(Math.random()*256);
return array;
}
printU8binary(randomU8array(32)); //example
/*
Max 32-bit value is (2**32)-1 = 4294967295 = ~0>>>0
Power of two: 1<<n = 2**n
1s bitmask: (1<<n)-1 (problem: (1<<32)-1 = 0) → (2**n)-1
>>> should be used when shifting, >> adds 1s at the left/upper if it's signed.
*/
function extractBits(value,start,count){
//Should start = 32-count-start?
return((value>>>start)&((2**count)-1))>>>0;
}