Related Articles
Find smallest positive number Y such that Bitwise AND of X and Y is Zero
• Last Updated : 18 Feb, 2020

Given an integer X. The task is to find the smallest positive number Y(> 0) such that X AND Y is zero.

Examples:

Input : X = 3
Output : 4
4 is the samllest positive number whose bitwise AND with 3 is zero

Input : X = 10
Output : 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :
There are 2 cases :

• If the binary representation of X contains all 1s, in that case, all the bits of Y should be 0 to make the result of AND operation is zero. Then X+1 is our answer which is the first positive integer.
• If the binary representation of X doesn’t contain all 1s, in that case, find the first position in X at which bit is 0. Then our answer will be power(2, position)

Below is the implementation of the above approach :

## C++

 `// C++ program to find smallest number Y for``// a given value of X such that X AND Y is zero``#include ``#define mod 1000000007``using` `namespace` `std;`` ` `// Method to find smallest number Y for``// a given value of X such that X AND Y is zero``int` `findSmallestNonZeroY(``int` `A_num)``{`` ` `    ``// Convert the number into its binary form``    ``string A_binary = bitset<8>(A_num).to_string();``    ``int` `B = 1;``    ``int` `length = A_binary.size();``    ``int` `no_ones = __builtin_popcount(A_num);`` ` `    ``// Case 1 : If all bits are ones,``    ``// then return the next number``    ``if` `(length == no_ones )``        ``return` `A_num + 1;`` ` `    ``// Case 2 : find the first 0-bit``    ``// index and return the Y``    ``for` `(``int` `i=0;i

## Java

 `// Java program to find smallest number Y for ``// a given value of X such that X AND Y is zero``import` `java.lang.*;`` ` `public` `class` `Main {``     ` `    ``// Method to find smallest number Y for ``    ``// a given value of X such that X AND Y is zero``    ``static` `long` `findSmallestNonZeroY(``long` `A_num)``    ``{``        ``// Convert the number into its binary form``        ``String A_binary = Long.toBinaryString(A_num);``        ``long` `B = ``1``;``        ``int` `len = A_binary.length();``        ``int` `no_ones = Long.bitCount(A_num);`` ` `        ``// Case 1 : If all bits are ones, ``        ``// then return the next number``        ``if` `(len == no_ones) {``            ``return` `A_num + ``1``;``        ``}`` ` `        ``// Case 2 : find the first 0-bit ``        ``// index and return the Y``        ``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;``    ``}``     ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``long` `X = findSmallestNonZeroY(``10``);``        ``System.out.println(X);``    ``}``}`

## Python3

 `# Python3 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``def` `findSmallestNonZeroY(A_num) :``     ` `    ``# Convert the number into its binary form``    ``A_binary ``=` `bin``(A_num) ``    ``B ``=` `1``    ``length ``=` `len``(A_binary);``    ``no_ones ``=` `(A_binary).count(``'1'``);``     ` `    ``# Case 1 : If all bits are ones, ``    ``# then return the next number``    ``if` `length ``=``=` `no_ones :``        ``return` `A_num ``+` `1``;``         ` `    ``# Case 2 : find the first 0-bit ``    ``# index and return the Y ``    ``for` `i ``in` `range``(length) : ``            ``ch ``=` `A_binary[length ``-` `i ``-` `1``];``             ` `            ``if` `(ch ``=``=` `'0'``) :``                ``B ``=` `pow``(``2.0``, i); ``                ``break``; ``                 ` `    ``return` `B; `` ` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:``    ``X ``=` `findSmallestNonZeroY(``10``);``    ``print``(X)``     ` `# This code is contributed by AnkitRai01`

## C#

 `// C# program to find smallest number Y for ``// a given value of X such that X AND Y is zero``using` `System;``     ` `class` `GFG ``{``     ` `    ``// Method to find smallest number Y for ``    ``// a given value of X such that X AND Y is zero``    ``static` `long` `findSmallestNonZeroY(``long` `A_num)``    ``{``        ``// Convert the number into its binary form``        ``String A_binary = Convert.ToString(A_num, 2);``        ``long` `B = 1;``        ``int` `len = A_binary.Length;``        ``int` `no_ones = bitCount(A_num);`` ` `        ``// Case 1 : If all bits are ones, ``        ``// then return the next number``        ``if` `(len == no_ones) ``        ``{``            ``return` `A_num + 1;``        ``}`` ` `        ``// Case 2 : find the first 0-bit ``        ``// index and return the Y``        ``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)``    ``{``        ``// To store the count``        ``// of set bits``        ``int` `setBits = 0;``        ``while` `(x != 0) ``        ``{``            ``x = x & (x - 1);``            ``setBits++;``        ``}``        ``return` `setBits;``    ``} ``     ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``long` `X = findSmallestNonZeroY(10);``        ``Console.WriteLine(X);``    ``}``}`` ` `// This code is contributed by 29AjayKumar`
Output:
```1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up