反向解析数字所包含的位移数

function trans(num) {
    const n = Number(num);
    if (n) {
        return n.toString(2).split('').reverse().map((l, i) => Number(l) ? Math.pow(2, i) : 0).filter(z => z);
    } else { return [] }
}

trans(2040)  //  [8, 16, 32, 64, 128, 256, 512, 1024];

trans(2048)  //  [2048]
原文地址:https://www.cnblogs.com/zhenguo-chen/p/14003525.html