JavaScript Program to Convert Decimal to Binary
Last Updated :
27 May, 2024
In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values.
Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to its equivalent binary representation.
Example 1:
Input : 7
Output : 111
Input : 10
Output : 1010
If the decimal number is 10.
- The remainder when 10 is divided by 2 is zero. Therefore, arr[0] = 0.
- Divide 10 by 2. The new number is 10/2 = 5.
- The remainder when 5 is divided by 2 is 1. Therefore, arr[1] = 1.
- Divide 5 by 2. The new number is 5/2 = 2.
- The remainder, when 2 is divided by 2, is zero. Therefore, arr[2] = 0.
- Divide 2 by 2. The new number is 2/2 = 1.
- The remainder when 1 is divided by 2 is 1. Therefore, arr[3] = 1.
- Divide 1 by 2. The new number is 1/2 = 0.
- Since the number becomes = 0.
- Print the array in reverse order. Therefore the equivalent binary number is 1010.
There are several approaches that can be used to Convert Decimal to Binary, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
The easiest and most basic way to convert decimal to binary is to use the unsigned right shift operator for converting the input number to an unsigned 32 bit integer. We make use of the toString method with argument 2 to specify that the output should be in base 2 i.e. binary.
Syntax:
function decimalToBinary(N) {
return (N >>> 0).toString(2);
}
Example: In this example, we are using the above-explained approach.
JavaScript
function decimalToBinary(N) {
return (N >>> 0).toString(2);
}
let N = 10;
let binary = decimalToBinary(N);
console.log(
"The binary representation of given number is:- " + binary);
OutputThe binary representation of given number is:- 1010
Approach 2: Using Loop and String Concatenation
In this approach, we use loop and string concatenation
- Initialize an empty string say binary.
- Run a while loop until the decimal number i.e. N is greater than 0.
- Inside the loop find the remainder of N by 2 and concatenate it with a binary string.
- Update the number N after dividing it by 2 in each iteration and use the floor function for it.
Syntax:
while (N > 0) {
binary = (N % 2) + binary;
N = Math.floor(N / 2);
}
Example: In this example, we are using the above explained approach.
JavaScript
function decimalToBinary(N) {
let binary = '';
while (N > 0) {
binary = (N % 2) + binary;
N = Math.floor(N / 2);
}
return binary;
}
let N = 10;
let binary = decimalToBinary(N);
console.log(
"The binary representation of given number is:- " + binary);
OutputThe binary representation of given number is:- 1010
Approach 3: Using toString(2) Method
The toString(2) method is a built-in JavaScript method available for number objects that converts a numeric value (decimal number) to its binary representation as a string.
Syntax:
num.toString(2)
Example: In this example, we are using the toString(2) method to convert our given numeric value to binary.
JavaScript
function decimalToBinary(val) {
return val.toString(2);
}
let num1 = 10;
let result = decimalToBinary(num1);
console.log("Binary representation:", result);
OutputBinary representation: 1010
Approach 4: Using Array and Math method
This approach converts a decimal number to binary using an array to store binary digits. It repeatedly divides numbers by 2 and adds remainders to the beginning of the array. The binary representation is then obtained by joining the array elements.
Syntax:
let arr = [];
for (; num1 > 0; num1 = Math.floor(num1 / 2)) {
arr.unshift(num1 % 2);
}
return arr.join("");
Example: In this example, we are using the above-explained approach.
JavaScript
function decimalToBinary(num1) {
if (num1 === 0) return "0";
let arr = [];
for (; num1 > 0; num1 = Math.floor(num1 / 2)) {
arr.unshift(num1 % 2);
}
return arr.join("");
}
let givenNumber = 10;
let result = decimalToBinary(givenNumber);
console.log("The binary representation is: " + result);
OutputThe binary representation is: 1010
Approach 5: Using Bitwise Operations and an Array
This approach involves using bitwise operations to convert a decimal number to binary. The idea is to repeatedly use the bitwise AND operator with 1 to extract the least significant bit of the number and then use the right shift operator to shift the bits to the right.
Example: In this example The function decimalToBinary converts a decimal number to binary using bitwise operations, pushing remainders to an array, then reverses and joins it to get the binary representation.
JavaScript
function decimalToBinary(N) {
if (N === 0) return "0";
let binaryArray = [];
while (N > 0) {
binaryArray.push(N & 1);
N = N >> 1;
}
return binaryArray.reverse().join('');
}
let number = 10;
let binary = decimalToBinary(number);
console.log("The binary representation of the given number is: " + binary);
OutputThe binary representation of the given number is: 1010
Similar Reads
JavaScript Program to Convert Decimal to Binary Using Recursion JavaScript allows us to convert decimal numbers to their binary equivalents using recursion, offering two distinct approaches. Through recursive division and concatenation, the first method constructs the binary representation directly. The second method leverages a string and recursion, providing a
2 min read
JavaScript Program to Convert a Number to Binary We are going to learn the conversion of a number to binary by using JavaScript, Convert a number to binary in JavaScript refers to the process of converting a decimal number into its binary representation, which uses only the digits 0 and 1 to represent the value Converting a number to binary in Jav
3 min read
JavaScript Program for Decimal to any base conversion In this JavaScript article, we will see how we can do decimal to any base conversion in JavaScript. The base can not be less than 2 and can not exceed 36, So we always have to find out the base of a decimal that lies in between this range, which is '2=< base <=36'. Example: Input: number = "11
5 min read
Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
How to convert binary to decimal in JavaScript? A binary number is a number expressed in the binary numeral system, which uses only two symbols 0 and 1. Decimal numbers are base 10 in a JavaScript number system that uses 10 digits from 0 to 9. We are given a number as Binary input we have to convert it into decimal in JavaScript and print the res
3 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
R Program to Convert Decimal to Binary, Octal, and Hexadecimal Converting decimal numbers to binary, octal, and hexadecimal is a fundamental operation in R Programming Language. These different numeral systems are used to represent numbers in other bases. In this article, we'll create programs to convert decimal to binary, octal & hexadecimal. Concepts rela
6 min read
Python Program to Convert Binary to Hexadecimal Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9
4 min read
Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek
3 min read