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