const numbers = [...Array(1000).keys()];
for (const number of numbers) {
let unit = 32;
const result = new Array(Math.floor(number / unit)).fill(unit);
while (unit >= 1) {
unit /= 2;
if (number & unit) result.push(unit);
}
}
for (const number of numbers) {
const bits = 5;
let unit = 1 << bits;
const result = new Array(number >> bits).fill(unit);
while (unit >= 1) {
unit >>= 1;
if (number & unit) result.push(unit);
}
}
for (const number of numbers) {
const maxUnit = 32;
const prefixLength = Math.floor(number / maxUnit);
// precalculate the length of the result array
let length = prefixLength, unit = maxUnit;
while (unit >= 1) {
unit /= 2;
if (number & unit) length++;
}
// allocate and fill array
const result = new Array(length).fill(maxUnit);
let i = prefixLength; unit = maxUnit;
while (unit >= 1) {
unit /= 2;
if (number & unit) result[i++] = unit;
}
}
for (const number of numbers) {
const bits = 5, maxUnit = 1 << bits;
const prefixLength = number >> bits;
// precalculate the length of the result array
let length = prefixLength, unit = maxUnit;
while (unit >= 1) {
unit >>= 1;
if (number & unit) length++;
}
// allocate and fill array
const result = new Array(length).fill(maxUnit);
let i = prefixLength; unit = maxUnit;
while (unit >= 1) {
unit >>= 1;
if (number & unit) result[i++] = unit;
}
}
function binary_buckets(n, power=1){
if (n === 0) {
return [];
}
const powers = binary_buckets(Math.floor(n / 2), power*2);
if (n % 2 === 1){
powers.push(power);
}
return powers;
}
function small_binary_buckets(n){
const tmp = new Array(Math.floor(n / 32)).fill(32);
return tmp.concat(binary_buckets(n % 32));
}
for (const number of numbers) {
const result = small_binary_buckets(number);
}
for (const num of numbers) {
const arr = new Array(Math.floor(num / 32)).fill(32);
const finalBits = [...(num % 32).toString(2)];
const { length } = finalBits;
const finalItems = finalBits
.map((char, i, finalBits) => char * (2 ** (length - i - 1)))
.filter(num => num);
}
for (const num of numbers) {
const arr = new Array(Math.floor(num / 32)).fill(32);
const { length } = arr;
Array.prototype.forEach.call((num % 32).toString(2), (char, i, finalBits) => {
if (char === '1') {
arr.push(2 ** (finalBits.length - i - 1));
}
});
}
for (number of numbers) {
let unit = 32;
const result = []
while (unit > 0) {
while (number >= unit) {
result.push(unit);
number -= unit;
}
unit >>= 1;
}
}