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 ; To store the count of set bits ; Driver code
function findSmallestNonZeroY ( A_num ) { let A_binary = ( A_num >>> 0 ) . toString ( 2 ) ; let B = 1 ; let len = A_binary . length ; let no_ones = bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( let i = 0 ; i < len ; i ++ ) { let ch = A_binary [ len - i - 1 ] ; if ( ch == ' ' ) { B = Math . floor ( Math . pow ( 2.0 , i ) ) ; break ; } } return B ; } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let X = findSmallestNonZeroY ( 10 ) ; document . write ( X ) ;