Decimal To Binary Algorithm
In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, normally four or eight. In byte-oriented systems (i.e. most modern computers), the term unpacked BCD normally imply a full byte for each digit (often including a sign), whereas packed BCD typically encodes two decimal digits within a individual byte by take advantage of the fact that four bits are enough to represent the range 0 to 9.
function decimalToBinary (num) {
var bin = []
while (num > 0) {
bin.unshift(num % 2)
num >>= 1 // basically /= 2 without remainder if any
}
console.log('The decimal in binary is ' + bin.join(''))
}
decimalToBinary(2)
decimalToBinary(7)
decimalToBinary(35)