Find smallest positive number Y such that Bitwise AND of X and Y is Zero | Java program to find smallest number Y for a given value of X such that 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
import java . lang . * ; public class Main { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Long . toBinaryString ( A_num ) ; long B = 1 ; int len = A_binary . length ( ) ; int no_ones = Long . bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary . charAt ( len - i - 1 ) ; if ( ch == '0' ) { B = ( long ) Math . pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } public static void main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; System . out . println ( X ) ; } }