js将正整数转化为二进制

//正整数转化为二进制
function divideBy2(decNumber) {
    var decStack = [];
    var rem;
    var decString = '';

    while (decNumber > 0) {
        rem = decNumber % 2;
        decStack.push(rem);
        decNumber = Math.floor(decNumber / 2);
    }

    while (decStack.length != 0) {
        decString += decStack.pop().toString();
    }
    return decString;
}
console.log(divideBy2(10)); //1010

 

posted @ 2017-01-19 14:03  张三的美丽家园  阅读(2401)  评论(0编辑  收藏  举报