Decimal to Binary Conversion in JavaScript
This post will discuss how to convert a decimal number to a binary number in JavaScript.
Binary is a base 2 number system that uses only two digits: 0 and 1. To convert a decimal (base 10) number to binary, we need to find the binary representation that is equivalent to the decimal value. For example, the decimal number 54 is equal to the binary number 110110. Some of the common ways are:
1. Using Number.toString() function
One of the easiest and most common ways to convert a number to binary in JavaScript is by using the Number.toString() function with a radix of 2. The toString() function converts a number to a string representation in a given base. The radix parameter specifies the base of the number system, which can be any integer from 2 to 36. For example, if we want to convert the number 54 to binary, we can write:
|
1 2 3 4 5 6 7 8 |
// A decimal number let num = 54; // Convert the number to binary using toString let bin = num.toString(2); // Prints '110110' console.log(bin); |
2. Using bitwise operators
Another way to convert a number to binary in JavaScript is by using bitwise operators, which manipulate individual bits of a number. Bitwise operators perform operations on the binary representation of a number, such as shifting, masking, or flipping bits. For example, if we want to convert the number 54 to binary using bitwise operators, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
let num = 54; let bin = ''; // Loop until num becomes zero while (num > 0) { // Get the rightmost bit of num using bitwise AND let bit = num & 1; // Prepend the bit to the binary string bin = bit + bin; // Right shift num by one bit using unsigned right shift operator num >>>= 1; } // Prints '110110' console.log(bin); |
3. Using Recursion
A third way to convert a number to binary in JavaScript is by using recursion, which is a technique where a function calls itself until a base case is reached. Recursion can be used to divide a problem into smaller subproblems that are easier to solve. We can use recursion to convert a number to binary by repeatedly dividing the number by 2 and appending the remainders to the binary representation. For example, if we want to convert the number 54 to binary using recursion, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Define a recursive function that takes a number as an argument function decimalToBinary(num) { // Base case: if num is zero, return an empty string if (num === 0) { return ''; } // Recursive case: divide num by 2 and get the remainder let remainder = num % 2; // Return the recursive call with the quotient and append the remainder return decimalToBinary(Math.floor(num / 2)) + remainder; } // A decimal number let num = 54; // Convert the number to binary using recursion let bin = decimalToBinary(num); // Prints '110110' console.log(bin); |
Recursion is a powerful and elegant way of converting a number to binary, but it may not be very efficient in terms of performance. That’s all about converting a decimal number to a binary number in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)