Previous smaller integer having one less number of set bits | function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position ' pos ' ; Driver program
function getFirstSetBitPos ( n ) { return parseInt ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ; } function previousSmallerInteger ( n ) { var pos = getFirstSetBitPos ( n ) ; return ( n & ~ ( 1 << ( pos - 1 ) ) ) ; } var n = 25 ; document . write ( " " + previousSmallerInteger ( n ) ) ;