2

I want to perform bitwise operations on any two buffers representing 64 bit unsigned ints. I apparently enjoy suffering so I decided to try this in JavaScript, which doesn't even support 64 bit math without BigInt conversion (which is not in my code yet). I know I must be approaching this in the most naïve, horrible way, but here it is.

The code doesn't get the right answer, but that's not really the problem. More like the way I'm going about it feels very inefficient. I knew I must be approaching the whole thing wrong so I stopped fixing this dumpster-fire of a function to ask the right way to do it. You'll get the idea:

function printHexStr(arr) {
  return Array.from(arr)
    .map((x) => {
      return x.toString(16).padStart(2, "0");
    })
    .join(" ");
}

function consolidateBuffer(buffer) {
  let val;
  let bufferDigits = []
  for (let i = 0; i < buffer.length; i++) {
    let digits = buffer[i].toString(16).split('')
    digits.forEach((digitStr, index) => {
      digits[index] = parseInt(digitStr, 16)
    })
    console.log('digits', digits)
    bufferDigits.push(...digits)
  }
  console.log('bufferDigits', bufferDigits)
  let list = []
  for (let i = bufferDigits.length - 1, j = 0; i >= 0; i--, j++) {
    console.log('i', i)
    console.log("bufferDigits[i]", bufferDigits[i], '{', bufferDigits[i].toString(16), '}');
    if (i == bufferDigits.length - 1) {
      val = bufferDigits[i];
    } else {
      console.log('val{', val, '} += bufferDigits[i]{', bufferDigits[i], '} * j{', j, '}^16{', Math.pow(16, j), '} == ', bufferDigits[i] * Math.pow(16, j))
      val += bufferDigits[i] * Math.pow(16, j);
    }
    list.push(bufferDigits[i].toString(16))
    console.log("list:", printHexStr(list), "=> val:", val);
  }
  console.log("Consolidated buffer:", bufferDigits, "to", val);
  return val;
}

let buff = new Uint8Array([0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f])
consolidateBuffer(buff)

The expected output would be 2242545357980376863. Someone please teach me a method of doing this that's less of a sin against mankind.

I would like to optionally input smaller buffers for 8, 16, 32 bit unsigned ints too.

J.Todd
  • 707
  • 1
  • 12
  • 34
  • so your using uint8 array with 8 values to represent uint64 because it would otherwise be to big to represent accurately with a normal variable? – Other Me Jun 06 '21 at 00:19
  • 1
    "*BigInt conversion (which is not in my code yet)*" - [you had that code already](https://stackoverflow.com/q/67651711/1048572), didn't you? Why don't you put it back to stop the suffering? – Bergi Jun 06 '21 at 00:26
  • @Bergi Yes. but the issue is, if we have smaller, say 16 or 32 bit values, and we want to avoid BigInts (for performance / overhead), this kind of function to combine multiple buffer view segments seems like a good idea. – J.Todd Jun 06 '21 at 02:15
  • That is, unless handling a 16 bit uint as a bigint happens to be more efficient just because of JS perhaps not having a lower level method for doing this conversion without bigint. – J.Todd Jun 06 '21 at 02:21
  • @J.Todd Sure, just use the typed array of the respective size – Bergi Jun 06 '21 at 09:07
  • 1
    @Bergi Ohh, that's how to do it... Just like the BigInts way of doing it.. *facepalm* – J.Todd Jun 06 '21 at 15:03
  • @Bergi Ah but before I ask another stupid question, I *will* have to do some kind of special conversion function like this to parse floats from the buffers, yes? – J.Todd Jun 06 '21 at 15:05
  • 1
    @J.Todd Nah, there's [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array) and [`Float64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array) for that :-) Or use a [`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) which gives you all options at once, even with control over endianness - not sure whether it has different performance than a typed array, probably not relevant for accessing individual elements though. – Bergi Jun 06 '21 at 15:10
  • @Bergi +100, I need to just read every single page of MSDN, I never know all the features – J.Todd Jun 06 '21 at 15:54

0 Answers0