Find smallest positive number Y such that Bitwise AND of X and Y is Zero | C # 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 ; To store the count of set bits ; Driver code
using System ; class GFG { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Convert . ToString ( A_num , 2 ) ; long B = 1 ; int len = A_binary . Length ; int no_ones = bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary [ len - i - 1 ] ; if ( ch == '0' ) { B = ( long ) Math . Pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; Console . WriteLine ( X ) ; } }