Convert Decimal to Binary
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:
- Start with an empty 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';
}
return toBinary(Math.floor(v / 2)) + (v % 2);
}
Here's what the procedure looks like with 42:
42 % 2 === 0
, '0'21 % 2 === 1
, '10'10 % 2 === 0
, '010'5 % 2 === 1
, '1010'2 % 2 === 0
, '01010'1
, '101010'