Set the Rightmost Unset Bit


The problem statement includes setting the rightmost unset bit of any positive integer N in C++. A bit is simply a binary digit of any number when represented in the form of a binary number.

A binary number is the numerical representation of any data in the form of 0 and 1 where every bit(digit) of the number represents the power of 2 starting with 2^0 from the unit digit.

Let's represent an integer 7 in the form of a binary number.

The binary representation of 7 will be 111.

These numbers can either be represented in 32-bit or 64-bit.

In this issue, a positive integer, say N will be provided, and our objective will be to set the rightmost unset bit, that is the rightmost 0 bit—to 1. And if every bit is set to 1, the number remains the same.

Let's use a few examples to better comprehend the problem statement.

Input

N=8

Output

9

Explanation : The binary representation of the integer 8 will be 1000. Here, the first unset bit which is 0 is the first bit. After changing the first unset bit, the binary number will be 1001 which is $\mathrm{2^{3}+2^{0}=9}$ So the output will be 9.

Input

N=19

Output

23

Explanation : The binary representation of 19 will be 10011. The first unset bit of the binary number 10011 is the third bit from right. The binary number after setting the first unset bit will be 10111 i.e.$\mathrm{2^{4}+2^{2}+2^{1}+2^{0}=16+4+2+1=23}$ , which is our output.

Input

N=3

Output

3

Explanation : The binary representation of 3 is 11. Since there is no unset bit in the binary number, remain the number unchanged. Thus, 3 will be our output.

Let’s learn the algorithm to solve this problem to set the rightmost unset bit in any number.

Algorithm

The idea behind setting the rightmost bit in any number is quite simple. We already know the concept of representing any number in the form of a binary number.

Let’s represent two consecutive numbers in the binary form and observe the pattern.

Assume numbers to be 9 and 10.

The binary representation of 9 is 1001.

And the binary representation of 10 is 1010.

Similarly let’s see for 12 and 13.

The binary representation of 12 is 1100.

And, the binary representation of 13 is 1101.

If we closely observe the pattern, we can conclude that the binary representation of the N+1 for any positive number N is just setting the first unset bit of N from right to 1 and unsetting all the set bits of N until we find the first unset bit.

This could be better explained with the expression:

$$\mathrm{2^{n}=2^{n-1}+2^{n-2}+....+2^{0}+1}$$

So if we apply the OR operation on the number and the number next to it, we can set the first unset bit of N to 1 without changing other bits as OR returns 0 if both the bits are 0 else it returns 1 if either of the bits is 1.

Before applying N | N+1 , we need to check if N has all the bits set or not. If it is then return the same number.

Approach

  • To set the rightmost unset bit in any number, we will write a function.

  • If all of the number’s bits are set, then just return the number without changing any of its bits.

  • We can check if all the bits of a number are set or not by simply taking AND of N and N+1 (N&(N+1)). If it is equal to 0 which means all the bits of N are set. Return the number N without changing any of its bits. If the output of (N&(N+1)) is other than 0, then we will apply OR operation on N and N+1(N | N+1) and return the number which we will get after applying the OR operation.

  • Let's assume if N=7 whose binary representation is 111. In this case all the bits of the N are set. When we take the AND of 111(i.e. 7) and 1000(i.e. 8), we will get 0 in all such cases.

  • In this way, we can set the first unset bit in any number N with the most efficient approach.

The C++ code for this approach:

Example

#include <bits/stdc++.h>

using namespace std;

//function to set the rightmost unset bit of N
int setbit(int N){
   
   if(N&(N+1)==0){  //if all the bits of N are set
      return N;
   }
   
   int x= N | N+1; //if not, then set the rightmost unset bit of N using the OR operation
   
   return x;
}

int main()
{
   int N;
   N=33;
   cout<<"The number after setting the rightmost unset bit of "<<N<<" is : "<<setbit(N)<<endl;
   
   N=123;
   cout<<"The number after setting the rightmost unset bit of "<<N<<" is : "<<setbit(N)<<endl;
   

   return 0;
}

Output

The number after setting the rightmost unset bit of 33 is : 35
The number after setting the rightmost unset bit of 123 is : 127

Time Complexity : O(1) , since constant time is required.

Space Complexity : O(1) , since no extra space is used.

Conclusion

The rightmost unset bit of any positive integer N can be set in C++. This article uses C++ to implement the technique. The approach used in the article to solve the issue operates in constant time, making it the best option. I believe that after reading this article, you will understand the topic better.

Updated on: 21-Aug-2023

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements