Previous smaller integer having one less number of set bits | Python3 implementation to find the previous smaller integer with 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 code
import math NEW_LINE def getFirstSetBitPos ( n ) : NEW_LINE INDENT return ( int ) ( math . log ( n & - n ) / math . log ( 2 ) ) + 1 NEW_LINE DEDENT def previousSmallerInteger ( n ) : NEW_LINE INDENT pos = getFirstSetBitPos ( n ) NEW_LINE DEDENT ' NEW_LINE INDENT return ( n & ~ ( 1 << ( pos - 1 ) ) ) NEW_LINE DEDENT n = 25 NEW_LINE print ( " Previous ▁ small ▁ Integer ▁ = ▁ " , previousSmallerInteger ( n ) ) NEW_LINE