Home > OS >  How to split JavaScript BigInt into 5-bit chunks?
How to split JavaScript BigInt into 5-bit chunks?

Time:01-05

Building off of this question How to get this PRNG to generate numbers within the range? , I am making it support BigInt (big values) in JavaScript, and I think I have it right (please correct me if I did it incorrectly):

const fetch = (x, o) => {
  if (x >= o) {
    return x
  } else {
    const v = (x * x) % o
    return (x <= (o / 2n)) ? v : o - v
  }
}

const fetchLarge = (x) => fetch(x, 43214321432143214321432143214321432143214321n)

// the last number can be anything.
const buildLarge = (x, o) => fetchLarge((fetchLarge(x)   o) % BigInt(Math.pow(32, 31)) ^ 101010101010104321432143214321n)

const j = 432143213214321432143214321432143214321n; // If you don't want duplicates, either i or j should stay fixed
let i = 1n
let invalid = [];
let valid = new Set;
while (i) {
  let x = buildLarge(i, j);
  if (valid.has(x)) {
    invalid.push([i, j, x]);
  } else {
    valid.add(x);
  }
  console.log(x)
  i  ;
}

console.log("invalid:", invalid);
console.log("valid:", [...valid]);
console.log("count of valid:", valid.size);

Sidenote: There should never be an invalid value.

But the main question is, given a value of x such as 40531205068036774067539981357810868028938588n, how can you divide it into an array of 5-bit values (array of integers between 0-31)?

Is it just as simple as doing x.toString(32) and then converting those letters to indices or something? Not sure if I'm doing this correctly.

CodePudding user response:

To create an array with numbers between 0-31 you can just divide and collect the remainders (which you would then convert to standard number):

function createArray(n, mod=32n) {
    if (!n) return [0];
    let arr = [];
    while (n) {
        arr.push(Number(n % mod));
        n /= mod;
    }
    return arr;
}

let result = createArray(40531205068036774067539981357810868028938588n);

console.log(result);

  •  Tags:  
  • Related