JavaScript Program for Binary Representation of Next Number
Last Updated :
28 May, 2024
Given a binary input that represents a binary representation of the positive number n, find a binary representation of n+1. We will try to find out this with the different approaches discussed below.
Examples:
Input : 10011
Output : 10100
Here n = (19)10 = (10011)2
next greater integer = (20)10 = (10100)2
Approach 1: Naive Approach
- Input is stored as a string to handle large numbers.
- Traverse the string from right to left.
- Convert consecutive 1s to 0s until the first 0 is found.
- Replace that 0 with 1. If no 0 is found, append 1 to the string.
Example: Below is the implementation of the above approach
JavaScript
function nextGreater(num) {
let i = num.length - 1;
while (i >= 0) {
if (num[i] === '0') {
num =
num.substring(0, i) +
'1' + num.substring(i + 1);
break;
} else {
num =
num.substring(0, i) +
'0' + num.substring(i + 1);
}
i--;
}
if (i < 0) {
num = '1' + num;
}
return num;
}
// Driver program to test above
let num = '1101';
console.log(
"Binary representation of next number = " +
nextGreater(num)
);
OutputBinary representation of next number = 1110
Approach 2: Optimised Approach
Convert the binary string num to an integer using the bitset class and its to_ulong method to obtain the integer representation.
Increment the integer by 1 to find the next greater integer.
Convert the incremented integer back to a binary string using the bitset class and its to_string method.
- Remove any leading zeros from the resulting binary string using string manipulation methods like erase and find_first_not_of, and then return the modified binary string.
Example: Below is the implementation of the above approach
JavaScript
// JavaScript Code for the above approach
function nextGreater(num) {
// Convert binary string to integer
const n = parseInt(num, 2);
// Increment integer by 1
const nextNum = n + 1;
// Convert integer back to binary string
let result = nextNum.toString(2);
// Remove leading zeros
result = result.replace(/^0+/, '');
return result;
}
// Driver code
const num = "110011";
console.log(
"Binary representation of next number =", nextGreater(num));
OutputBinary representation of next number = 110100
Approach 3: Bit Manipulation with Right Shift Operator
Bit manipulation with the right shift operator calculates the next number's binary representation by adding 1 to the input number and converting it to binary using the `toString` method. The right shift operator ensures the result is an integer.
Example: The function nextBinaryNumberBitManipulation computes the next binary number by adding 1 to the input and converts it to a binary string.
JavaScript
function nextBinaryNumberBitManipulation(num) {
return ((num + 1) >> 0).toString(2);
}
let number = 5;
console.log(nextBinaryNumberBitManipulation(number));
Similar Reads
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 to Find the Next Power of Two Given a positive integer, find the next power of two greater than the given number using JavaScript. Examples: Input: N = 6Output: 8Explanation: The next power of two greater than 6 is 8.Input: N = 16Output: 16Explanation: 16 is already a power of two, so the next power of two is also 16.Below are t
3 min read
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 Decimal to Binary 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 i
5 min read
JavaScript Program to Check if a Number has Bits in an Alternate Pattern JavaScript can be used to assess whether a given number follows an alternating pattern in its binary representation. By examining the binary digits of the number, one can determine if the sequence alternates between 0s and 1s, aiding in understanding the binary structure of the input. Examples: Inpu
2 min read