Given two integers **A** and **B**, the task is to find an integer **X** such that **(X XOR A)** is minimum possible and the count of set bit in **X** is equal to the count of set bits in **B**.

**Examples:**

Input:A = 3, B = 5

Output:3

Binary(A) = Binary(3) = 011

Binary(B) = Binary(5) = 101

The XOR will be minimum when M = 3

i.e. (3 XOR 3) = 0 and the number

of set bits in 3 is equal

to the number of set bits in 5.

Input:A = 7, B = 12

Output:6

**Approach:** It is known that the xor of an element with itself is 0. So, try to generate M’s binary representation as close to A as possible. Traverse from the most significant bit in A to the least significant bit and if a bit is set at the current position then it also needs to be set in the required number in order to minimize the XOR but the number of bits set has to be equal to the number of set bits in B. So, when the count of set bits in the required number has reached the count of set bits in B then the rest of the bits have to be 0.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the value x ` `// such that (x XOR a) is minimum ` `// and the number of set bits in x ` `// is equal to the number ` `// of set bits in b ` `int` `minVal(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Count of set-bits in bit ` ` ` `int` `setBits = __builtin_popcount(b); ` ` ` `int` `ans = 0; ` ` ` ` ` `for` `(` `int` `i = 30; i >= 0; i--) { ` ` ` `int` `mask = 1 << i; ` ` ` `bool` `set = a & mask; ` ` ` ` ` `// If i'th bit is set also set the ` ` ` `// same bit in the required number ` ` ` `if` `(set && setBits > 0) { ` ` ` `ans |= (1 << i); ` ` ` ` ` `// Decrease the count of setbits ` ` ` `// in b as the count of set bits ` ` ` `// in the required number has to be ` ` ` `// equal to the count of set bits in b ` ` ` `setBits--; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 3, b = 5; ` ` ` ` ` `cout << minVal(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to get no of set ` ` ` `// bits in binary representation ` ` ` `// of positive integer n ` ` ` `static` `int` `countSetBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `count += n & ` `1` `; ` ` ` `n >>= ` `1` `; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` `// Function to return the value x ` `// such that (x XOR a) is minimum ` `// and the number of set bits in x ` `// is equal to the number ` `// of set bits in b ` `static` `int` `minVal(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Count of set-bits in bit ` ` ` `int` `setBits = countSetBits(b); ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `30` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` `int` `mask = ` `1` `<< i; ` ` ` ` ` `// If i'th bit is set also set the ` ` ` `// same bit in the required number ` ` ` `if` `((a & mask) > ` `0` `&& setBits > ` `0` `) ` ` ` `{ ` ` ` `ans |= (` `1` `<< i); ` ` ` ` ` `// Decrease the count of setbits ` ` ` `// in b as the count of set bits ` ` ` `// in the required number has to be ` ` ` `// equal to the count of set bits in b ` ` ` `setBits--; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `3` `, b = ` `5` `; ` ` ` ` ` `System.out.println(minVal(a, b)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the value x ` `# such that (x XOR a) is minimum ` `# and the number of set bits in x ` `# is equal to the number ` `# of set bits in b ` `def` `minVal(a, b) : ` ` ` ` ` `# Count of set-bits in bit ` ` ` `setBits ` `=` `bin` `(b).count(` `'1'` `); ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `for` `i ` `in` `range` `(` `30` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` `mask ` `=` `(` `1` `<< i); ` ` ` `s ` `=` `(a & mask); ` ` ` ` ` `# If i'th bit is set also set the ` ` ` `# same bit in the required number ` ` ` `if` `(s ` `and` `setBits > ` `0` `) : ` ` ` `ans |` `=` `(` `1` `<< i); ` ` ` ` ` `# Decrease the count of setbits ` ` ` `# in b as the count of set bits ` ` ` `# in the required number has to be ` ` ` `# equal to the count of set bits in b ` ` ` `setBits ` `-` `=` `1` `; ` ` ` ` ` `return` `ans; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `3` `; b ` `=` `5` `; ` ` ` ` ` `print` `(minVal(a, b)); ` ` ` `# This code is contributed by kanugargng ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to get no of set ` ` ` `// bits in binary representation ` ` ` `// of positive integer n ` ` ` `static` `int` `countSetBits(` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `count += n & 1; ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` `// Function to return the value x ` `// such that (x XOR a) is minimum ` `// and the number of set bits in x ` `// is equal to the number ` `// of set bits in b ` `static` `int` `minVal(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Count of set-bits in bit ` ` ` `int` `setBits = countSetBits(b); ` ` ` `int` `ans = 0; ` ` ` ` ` `for` `(` `int` `i = 30; i >= 0; i--) ` ` ` `{ ` ` ` `int` `mask = 1 << i; ` ` ` ` ` `// If i'th bit is set also set the ` ` ` `// same bit in the required number ` ` ` `if` `((a & mask) > 0 && setBits > 0) ` ` ` `{ ` ` ` ` ` `ans |= (1 << i); ` ` ` ` ` `// Decrease the count of setbits ` ` ` `// in b as the count of set bits ` ` ` `// in the required number has to be ` ` ` `// equal to the count of set bits in b ` ` ` `setBits--; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 3, b = 5; ` ` ` ` ` `Console.Write(minVal(a, b)); ` `} ` `} ` ` ` `// This code is contributed by Mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(log(N))

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.

## Recommended Posts:

- Count minimum bits to flip such that XOR of A and B equal to C
- Check if bits of a number has count of consecutive set bits in increasing order
- Count pairs with set bits sum equal to K
- Count pairs in an array such that both elements has equal set bits
- Find the minimum number of operations required to make all array elements equal
- Find the node whose sum with X has minimum set bits
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count total bits in a number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count Set-bits of number using Recursion
- Count unset bits of a number
- Minimum flips required to maximize a number with k set bits
- Count number of set bits in a range using bitset
- Count number of bits to be flipped to convert A to B
- Count number of bits to be flipped to convert A to B | Set-2
- Count of pairs in an Array with same number of set bits
- Count pairs of elements such that number of set bits in their OR is B[i]
- Program to count number of set bits in an (big) array
- Count number of bits changed after adding 1 to given N
- Count pairs of elements such that number of set bits in their AND is B[i]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.