bittest | const calculate = (n) => {
const bitsRequired = Math.floor(Math.log2(n) + 1);
const result = [];
for(let i = bitsRequired - 1; i >= 0; i--) {
const current = 1 << i;
if((n & current) > 0) {
result.push(bitsRequired - i);
}
}
return [result.length, ...result];
}
const arrLength = 1000
const maxStrSize = 100000
const numArr = Array.from({length: arrLength}, (_, i) => Math.floor(Math.random() * maxStrSize))
console.log('numArr', numArr)
console.log('numArr.map(e => calculate(e))', numArr.map(e => calculate(e)))
| ready |
bittest2 | function countBitsAndPositions(n) {
const binary = n.toString(2);
const bitPositions = [];
let count = 0;
for (let i = 0; i < binary.length; i++) {
if (binary[i] === '1') {
count++;
bitPositions.push(i + 1);
}
}
return [count, ...bitPositions];
}
const arrLength = 1000
const maxStrSize = 100000
const numArr = Array.from({length: arrLength}, (_, i) => Math.floor(Math.random() * maxStrSize))
console.log('numArr', numArr)
console.log('numArr.map(e => countBitsAndPositions(e))', numArr.map(e => countBitsAndPositions(e)))
| ready |