Set the rightmost unset bit | C # implementation to set the rightmost unset bit ; Function to find the position of rightmost set bit ; If n = 0 , return 1 ; If all bits of ' n ' are set ; Position of rightmost unset bit in ' n ' passing ~ n as argument ; Set the bit at position ' pos ' ; Driver code
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 ) ) ; } }