Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n. If there are no unset bits, then just leave the number as it is.
Examples:
Input : 21
Output : 23
(21)10 = (10101)2
Rightmost unset bit is at position 2(from right) as
highlighted in the binary representation of 21.
(23)10 = (10111)2
The bit at position 2 has been set.
Input : 15
Output : 15
Approach: Following are the steps:
- If n = 0, return 1.
- If all bits of n are set, return n. Refer to this post.
- Else perform bitwise not on the given number(operation equivalent to 1’s complement). Let it be num = ~n.
- Get the position of rightmost set bit of num. Let the position be pos.
- Return (1 << (pos – 1)) | n.
C++
#include <bits/stdc++.h>
using namespace std;
int getPosOfRightmostSetBit( int n)
{
return log2(n&-n)+1;
}
int setRightmostUnsetBit( int n)
{
if (n == 0)
return 1;
if ((n & (n + 1)) == 0)
return n;
int pos = getPosOfRightmostSetBit(~n);
return ((1 << (pos - 1)) | n);
}
int main()
{
int n = 21;
cout << setRightmostUnsetBit(n);
return 0;
}
|
Java
class GFG {
static int getPosOfRightmostSetBit( int n)
{
return ( int )((Math.log10(n & -n)) / (Math.log10( 2 ))) + 1 ;
}
static int setRightmostUnsetBit( int n)
{
if (n == 0 )
return 1 ;
if ((n & (n + 1 )) == 0 )
return n;
int pos = getPosOfRightmostSetBit(~n);
return (( 1 << (pos - 1 )) | n);
}
public static void main(String arg[]) {
int n = 21 ;
System.out.print(setRightmostUnsetBit(n));
}
}
|
Python3
import math
def getPosOfRightmostSetBit(n):
return int (math.log2(n& - n) + 1 )
def setRightmostUnsetBit(n):
if (n = = 0 ):
return 1
if ((n & (n + 1 )) = = 0 ):
return n
pos = getPosOfRightmostSetBit(~n)
return (( 1 << (pos - 1 )) | n)
n = 21
print (setRightmostUnsetBit(n))
|
C#
using System;
class GFG{
static int getPosOfRightmostSetBit( int n)
{
return ( int )((Math.Log10(n & -n)) /
(Math.Log10(2))) + 1;
}
static int setRightmostUnsetBit( int n)
{
if (n == 0)
return 1;
if ((n & (n + 1)) == 0)
return n;
int pos = getPosOfRightmostSetBit(~n);
return ((1 << (pos - 1)) | n);
}
public static void Main(String []arg)
{
int n = 21;
Console.Write(setRightmostUnsetBit(n));
}
}
|
Javascript
<script>
function getPosOfRightmostSetBit(n)
{
return ((Math.log10(n & -n)) / (Math.log10(2))) + 1;
}
function setRightmostUnsetBit(n)
{
if (n == 0)
return 1;
if ((n & (n + 1)) == 0)
return n;
let pos = getPosOfRightmostSetBit(~n);
return ((1 << (pos - 1)) | n);
}
let n = 21;
document.write(setRightmostUnsetBit(n));
</script>
|
Output:
23
Alternate Implementation
The idea is to use Integer.toBinaryString()
C++
#include <bits/stdc++.h>
using namespace std;
string getBinary( int n)
{
string binary = bitset<32>(n).to_string();
binary.erase(0, binary.find_first_not_of( '0' ));
return binary;
}
void setMostRightUnset( int a)
{
int x = a ^ (a - 1);
cout << getBinary(x) << endl;
x = x & a;
cout << getBinary(x) << endl;
x = x >> 1;
int b = a | x;
cout << "before setting bit " << getBinary(a) << endl;
cout << "after setting bit " << getBinary(b) << endl;
}
int main()
{
int a = 21;
setMostRightUnset(a);
return 0;
}
|
Java
import java.io.*;
import java.util.Scanner;
public class SetMostRightUnsetBit {
public static void main(String args[])
{
int a = 21 ;
setMostRightUnset(a);
}
private static void setMostRightUnset( int a)
{
int x = a ^ (a - 1 );
System.out.println(Integer.toBinaryString(x));
x = x & a;
System.out.println(Integer.toBinaryString(x));
x = x >> 1 ;
int b = a | x;
System.out.println( "before setting bit " +
Integer.toBinaryString(a));
System.out.println( "after setting bit " +
Integer.toBinaryString(b));
}
}
|
Python3
def setMostRightUnset(a):
x = a ^ (a - 1 )
print ( bin (x)[ 2 :])
x = x & a
print ( bin (x)[ 2 :])
x = x >> 1
b = a | x
print ( "before setting bit " , bin (a)[ 2 :])
print ( "after setting bit " , bin (b)[ 2 :])
if __name__ = = '__main__' :
a = 21
setMostRightUnset(a)
|
C#
using System;
class SetMostRightUnsetBit{
public static void Main(String []args)
{
int a = 21;
setMostRightUnset(a);
}
private static void setMostRightUnset( int a)
{
int x = a ^ (a - 1);
Console.WriteLine(Convert.ToString(x));
x = x & a;
Console.WriteLine(Convert.ToString(x));
x = x >> 1;
int b = a | x;
Console.WriteLine( "before setting bit " +
Convert.ToString(a, 2));
Console.WriteLine( "after setting bit " +
Convert.ToString(b, 2));
}
}
|
Javascript
<script>
function setMostRightUnset(a)
{
let x = a ^ (a - 1);
document.write((x >>> 0).toString(2)+ "<br>" );
x = x & a;
document.write((x >>> 0).toString(2)+ "<br>" );
x = x >> 1;
let b = a | x;
document.write( "before setting bit " +
(a >>> 0).toString(2)+ "<br>" );
document.write( "after setting bit " +
(b >>> 0).toString(2)+ "<br>" );
}
let a = 21;
setMostRightUnset(a);
</script>
|
Output:
1
1
before setting bit 10101
after setting bit 10101
Another Approach:
The idea is x|(x+1) sets the lowest(rightmost) unset bit but it may set a leading zero bit if the given number doesn’t contain any unset bits , for example
if all bits set
15 is 1111 = 00001111
16 is 10000 = 00010000
_________
x|(x+1) 00011111
on performing x|(x+1) it converts to 00011111
but here we should only consider only 1111 and return 1111 as there are no unset bits.
else
13 is 1101 = 00001101
14 is 1110 = 00001110
_________
x|(x+1) 00001111
on performing x|(x+1) it converts to 00011111 which is required number.
So we can eliminate this type of redundant operation by checking if there are any zeroes in the given number. We observe only number whose all bits set are of form 2k -1 have no unset bits in them so by catching these number we can perform our idea.
C++
#include <bits/stdc++.h>
using namespace std;
int setRightmostUnsetBit( int n)
{
if (!(n & (n + 1)))
return n;
return n | (n + 1);
}
int main()
{
int n = 21;
cout << setRightmostUnsetBit(n);
return 0;
}
|
Java
class GFG {
static int setRightmostUnsetBit( int n)
{
if ((n & (n + 1 )) == 0 )
return n;
return n | (n + 1 );
}
public static void main(String arg[])
{
int n = 21 ;
System.out.print(setRightmostUnsetBit(n));
}
}
|
Python3
import math
def setRightmostUnsetBit(n):
if ((n & (n + 1 )) = = 0 ):
return n
return n | (n + 1 )
n = 21
print (setRightmostUnsetBit(n))
|
C#
using System;
class GFG {
static int setRightmostUnsetBit( int n)
{
if ((n & (n + 1)) == 0)
return n;
return n | (n + 1);
}
public static void Main(String[] arg)
{
int n = 21;
Console.Write(setRightmostUnsetBit(n));
}
}
|
Javascript
function setRightmostUnsetBit(n)
{
if (!(n & (n + 1)))
return n;
return n | (n + 1);
}
let n = 21;
console.log(setRightmostUnsetBit(n));
|
Output:
23
Time complexity: O(1)
Space Complexity: O(1)
This article is contributed by Ayush Jauhari and Kasina Dheeraj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...