Find smallest positive number Y such that Bitwise AND of X and Y is Zero | Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; Driver Code
def findSmallestNonZeroY ( A_num ) : NEW_LINE INDENT A_binary = bin ( A_num ) NEW_LINE B = 1 NEW_LINE length = len ( A_binary ) ; NEW_LINE no_ones = ( A_binary ) . count ( '1' ) ; NEW_LINE if length == no_ones : NEW_LINE INDENT return A_num + 1 ; NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT ch = A_binary [ length - i - 1 ] ; NEW_LINE if ( ch == '0' ) : NEW_LINE INDENT B = pow ( 2.0 , i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return B ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = findSmallestNonZeroY ( 10 ) ; NEW_LINE print ( X ) NEW_LINE DEDENT