A Decimal is a term that describes the base-10 number system.
A Binary is a term that describes the base-2 number system.
Step 1: Divide the decimal number by 2 and write down the remainder value.
Step 2: Divide the quotient by 16 and write the remainder again.
Step 3: Repeat steps 1 and 2, until you get the quotient as 0.
Step 4: Now, write the remainders in reverse order, starting with the last remainder.
Step 5: Another way to read this is that the binary number’s Least Significant Bit (LSB) is at the top and its Most Significant Bit (MSB) is at the bottom.
Below is a visualization to help you understand the above steps to convert decimal to binary.
Example: Covert decimal number into binary.
The following code shows the conversion from decimal to binary number.
let n = 13;console.log(n.toString(2));
Line 1: Declaring a variable n
.
Line 2: The console.log()
method writes a message to the console, and n.toString(2)
converts the number to the string using base 2.
Free Resources