Set the rightmost off bit | JavaScript program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver program to test above
function setRightmostUnsetBit ( n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; return n | ( n + 1 ) ; } let n = 21 ; document . write ( setRightmostUnsetBit ( n ) ) ;