Convert Decimal to Binary

May 7, 2021

A binary number is a number expressed in base-2, as opposed to conventional decimal numbers in base-10.

Below is a live calculator.

Decimal Binary

How the Calculator Works

Converting decimal numbers to binary in JavaScript is easy. For example, `let x = 42` creates a new variable `x` that contains the base 10 number `42`. JavaScript numbers have a `toString()` method that takes a `radix` parameter. Calling `x.toString(2)` tells JavaScript to convert `x` to a string containing the binary representation of `42`.

``````let x = 42;

x.toString(2); // '101010'``````

The `toString()` method also handles non-integers and negative numbers. For example:

``````x = 3.14;
x.toString(2); // '11.001000111101011100001010001111010111000010100011111'

x = -7;
x.string(2); // '-111'``````

Mental Math

How do you quickly convert `7` to `111` in your head? It takes some practice to make it easy, but here's the procedure to convert a positive integer `v` to a binary string:

• Take `v` modulo 2 and add it to the end of the string
• Repeat with `Math.floor(v / 2)` until you get to 0 or 1

Below is a JavaScript function that implements the above procedure:

``````function toBinary(v, str) {
if (!Number.isSafeInteger(v) || v < 0) {
throw new Error('v must be a non-negative integer');
}
if (v === 1) {
return '1';
}
if (v === 0) {
return '0';
}
1. `42 % 2 === 0`, '0'
2. `21 % 2 === 1`, '10'
3. `10 % 2 === 0`, '010'
4. `5 % 2 === 1`, '1010'
5. `2 % 2 === 0`, '01010'
6. `1`, '101010'