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 ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; int findSmallestNonZeroY ( int A_num ) { string A_binary = bitset < 8 > ( A_num ) . to_string ( ) ; int B = 1 ; int length = A_binary . size ( ) ; int no_ones = __builtin_popcount ( A_num ) ; if ( length == no_ones ) return A_num + 1 ; for ( int i = 0 ; i < length ; i ++ ) { char ch = A_binary [ length - i - 1 ] ; if ( ch == '0' ) { B = pow ( 2.0 , i ) ; break ; } } return B ; } int main ( ) { int X = findSmallestNonZeroY ( 10 ) ; cout << X ; }