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