Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to split JavaScript BigInt into 5-bit chunks?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!