export class Binary {
    constructor(num, bits) {
        this.num = num ? num : 0;
        this.bits = bits ? bits : 0;
    }
    decimalToBinary(num, bits) {
        const resArry = [];
        const xresArry = [];
        let i = 0;
        // 除2取余
        for (; num > 0;) {
            resArry.push(num % 2);
            // eslint-disable-next-line radix
            num = parseInt((num / 2).toString());
            i++;
        }
        // 倒序排列
        for (let j = i - 1; j >= 0; j--) {
            xresArry.push(resArry[j]);
        }
        // 报错
        if (bits < xresArry.length) {
            console.log('控制位小于转换位数');
        }
        // 补0操作
        if (bits) {
            for (let r = xresArry.length; r < bits; r++) {
                xresArry.unshift('0');
            }
        }
        return xresArry.join().replace(/,/g, '');
    }
    convertToBinary() {
        const value = Number(this.num);
        return this.decimalToBinary(Math.abs(value), this.bits);
    }
}
