Algorithm to find bucket and item indices from power of two bins?
I think your code is too complex and can be simplified.
Also, you wrote &quot;using only primitive operations and values (i.e. not fancy array.map and such...&quot;, yet you use array.forEach.  Where is the &quot;line&quot;?
I hope this is what you are looking for.
Note:

up-to 32 -&gt; one bucket
32 up-to 32*32 -&gt; another bucket
3232 up-to 3232*32 -&gt; another bucket
etc.

Only after done, I got @superb rain's [i &gt;&gt; 5, i &amp; 31]...
Perhaps I'll implement that in the future.
However, my code allows for &quot;bucket-of-buckets&quot;.
The heart of my code is the recursive function:
function recBucket(number, size) {
  if(number&lt;size) return [number];
  else {
    var whole=Math.floor(number/size);
    return [recBucket(whole, size), number-whole*size].flat();
  }
};

Here is a snippet that uses and builds on that:


var numbers = [1, 2, 3, 31, 32, 33, 100, 201, 1023, 1024, 5555];

function recBucket(number, size) {
  if(number&lt;size) return [number];
  else {
    var whole=Math.floor(number/size);
    return [recBucket(whole, size), number-whole*size].flat();
  }
};

console.log("as is:");
numbers.forEach(n=&gt;console.log(n+" = "+recBucket(n, 32).join(":")));

function minBuckets(number, size, buckets) {
  var result=recBucket(number, size);
  while(result.length&lt;buckets) result.unshift(0);
  return result;
};

console.log("min 2 buckets:");
numbers.forEach(n=&gt;console.log(n+" = "+minBuckets(n, 32,2).join(":")));

console.log("min 4 buckets:");
numbers.forEach(n=&gt;console.log(n+" = "+minBuckets(n, 32,4).join(":")));
.as-console-wrapper { max-height: 100% !important; top: 0; }