Skip to content
Related Articles

Related Articles

Set the rightmost unset bit

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 06 Nov, 2021

Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n
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 : 2
Output : 3

 

One method is discussed in this article
This post discusses another method.
Let the input number be n. n+1 would have all the bits flipped after the rightmost unset bit (including the unset bit). So, doing n|(n+1) would give us the required result. 
 

C++




#include <stdio.h>
 
// sets the rightmost unset bit
// of n and returns the result
int fun(unsigned int n)
{
    return n | (n + 1);
}
 
// Driver Code
int main()
{
    int n = 5;
    printf("The number after setting the");
    printf(" rightmost unset bit %d", fun(n));
    return 0;
}

Java




class GFG {
     
    // sets the rightmost unset bit
    // of n and returns the result
    static int fun(int n)
    {
        return n | (n + 1);
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        System.out.printf("The number "
                  + "after setting the");
        System.out.printf(" rightmost "
              + " unset bit %d", fun(n));
    }
}
 
// This code is contributed by Smitha

Python3




# sets the rightmost unset bit
# of n and returns the result
def fun(n):
 
    return n | (n + 1)
 
# Driver Code
n = 5
print("The number after setting the", end="")
print(" rightmost unset bit ", fun(n))
 
# This code is contributed by Smitha

C#




using System;
 
class GFG {
     
    // sets the rightmost unset bit
    // of n and returns the result
    static int fun(int n)
    {
        return n | (n + 1);
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 5;
        Console.Write("The number "
            + "after setting the");
        Console.Write(" rightmost "
           + "unset bit "+ fun(n));
    }
}
 
// This code is contributed by Smitha

PHP




<?php
// PHP program to Set the rightmost
// unset bit
 
// sets the rightmost unset bit
// of n and returns the result
function fun($n)
{
    return $n | ($n + 1);
}
 
    // Driver Code
    $n = 5;
    echo "The number after setting the";
    echo " rightmost unset bit ", fun($n);
     
// This code is contributed m_kit
?>

Javascript




<script>
// sets the rightmost unset bit
// of n and returns the result
function fun(n)
{
    return n | (n + 1);
}
 
// Driver Code
    let n = 5;
    document.write("The number after setting the");
    document.write(" rightmost unset bit ", fun(n));
 
// This code is contributed by Manoj.
</script>

Output : 
 

The number after setting the rightmost unset bit 7

 Time Complexity: O(1)

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!