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;
}
}
for (number of numbers) {
let unit = 32;
const result = []
while (unit > 0) {
while (number >= unit) {
result.push(unit);
number -= unit;
}
unit >>= 1;
}
}