Set the rightmost off bit - GeeksforGeeks

05 Nov, 2021
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++
Java
Python 3
C#
PHP
Javascript
// 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 aboveint main(){    int n = 21;    cout << setRightmostUnsetBit(n);    return 0;}
//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));    } }
# 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
// 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));    }  }
<?php// PHP program to set the// rightmost unset bitfunction 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?>
<script>// JavaScript program to set the rightmost unset bitfunction setRightmostUnsetBit(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    let n = 21;    document.write(setRightmostUnsetBit(n)); // This code is contributed by Manoj.</script>
23
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ankthon
ukasp
Shivi_Aggarwal
tufan_gupta2000
Shubham__Ranjan
mank1083
rohan07
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
How to turn off a particular bit in a number?
Extract 'k' bits from a given position in a number.
Swap two nibbles in a byte
Reverse actual bits of the given number
Write an Efficient Method to Check if a Number is Multiple of 3
Count total bits in a number
Calculate XOR from 1 to n.
Bit Tricks for Competitive Programming
Convert decimal fraction to binary number