Set the rightmost off bit

Write a program that sets the rightmost 0 bit of an integer.

Examples :

Input:  12 (00...01100)
Output: 13 (00...01101)

Input:  7 (00...00111)
Output: 15 (00...01111)


If we add 1 to a number, all set bits after rightmost unset (or zero bit) become 0 and the rightmost unset bit becomes 1.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to set the rightmost unset bit
#include<iostream>
using namespace std; 
    
int setRightmostUnsetBit(int n) 
{     
    // If all bits are set  
    if ((n & (n + 1)) == 0)     
        return n; 
        
    // Set rightmost 0 bit
    return n | (n+1);     
    
// Driver program to test above 
int main() 
    int n = 21; 
    cout << setRightmostUnsetBit(n); 
    return 0; 
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java  program to set the rightmost unset bit
  
public class GFG {
  
    static int setRightmostUnsetBit(int n) 
    {     
     // If all bits are set  
     if ((n & (n + 1)) == 0)     
         return n; 
          
     // Set rightmost 0 bit
     return n | (n+1);     
    
  
    //Driver program to test above 
    public static void main(String[] args) {
  
         int n = 21
         System.out.println(setRightmostUnsetBit(n)); 
    }
  
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to set the 
# rightmost unset bit 
  
def setRightmostUnsetBit(n) :
  
    # If all bits are set 
    if n & (n + 1) == 0 :
        return n
      
    # Set rightmost 0 bit 
    return n | (n+1)
  
# Driver code     
if __name__ == "__main__" :
  
    n = 21
    print(setRightmostUnsetBit(n))
  
# This code is contributed
# by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to set the rightmost unset bit
  
using System;
  
public class GFG {
   
    static int setRightmostUnsetBit(int n) 
    {     
     // If all bits are set  
     if ((n & (n + 1)) == 0)     
         return n; 
           
     // Set rightmost 0 bit
     return n | (n+1);     
    
   
    //Driver program to test above 
    public static void Main() {
   
         int n = 21; 
         Console.WriteLine(setRightmostUnsetBit(n)); 
    }
   
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to set the 
// rightmost unset bit 
function setRightmostUnsetBit($n
    // If all bits are set 
    if (($n & ($n + 1)) == 0) 
        return $n
          
    // Set rightmost 0 bit 
    return $n | ($n + 1); 
      
// Driver Code
$n = 21; 
echo setRightmostUnsetBit($n); 
      
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

23


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.