v1
- by vyznev 9/15/202000
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const numbers = [...Array(1000).keys()];
delete caserun single casemove downdrag and drop case


ready



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);
	}
}
delete caserun single casemove upmove downdrag and drop case


ready



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);
	}
}
delete caserun single casemove upmove downdrag and drop case


ready



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;
	}
}
delete caserun single casemove upmove downdrag and drop case


ready



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;
	}
}
delete caserun single casemove upmove downdrag and drop case


ready



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);
}
delete caserun single casemove upmove downdrag and drop case


ready



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);
}
delete caserun single casemove upmove downdrag and drop case


ready



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));
		}
	});
}
delete caserun single casemove updrag and drop case


ready



for (number of numbers) {
	let unit = 32;
	const result = []
	while (unit > 0) {
		while (number >= unit) {
			result.push(unit);
			number -= unit;
		}
		unit >>= 1;
	}
}
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN