# Choose X such that (A xor X) + (B xor X) is minimized

Given two integers **A** and **B**. The task is to choose an integer **X** such that **(A xor X) + (B xor X)** is minimum possible.

**Examples:**

Input:A = 2, B = 3

Output:X = 2, Sum = 1

Input:A = 7, B = 8

Output:X = 0, Sum = 15

A **simple solution** is to generate all possible sum by taking xor of **A** and **B** with all possible value of **X ≤ min(A, B)**. To generate all possible sums it would take **O(N)** time where **N = min(A, B)**.

An **efficient solution** is based on the fact that the number **X** will contain the set bits only at that index where both **A** and **B** contains a set bit such that after xor operation with **X** that bit will be unset. This would take only **O(Log N)** time.

**Other cases:** If at a particular index one or both the numbers contain **0** (unset bit) and the number **X** contains **1** (set bit) then **0** will be set after xor with **X** in **A** and **B** then the sum couldn't be minimized .

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the integer X such that ` `// (A xor X) + (B ^ X) is minimized ` `int` `findX(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `j = 0, x = 0; ` ` ` ` ` `// While either A or B is non-zero ` ` ` `while` `(A || B) { ` ` ` ` ` `// Position at which both A and B ` ` ` `// have a set bit ` ` ` `if` `((A & 1) && (B & 1)) { ` ` ` ` ` `// Inserting a set bit in x ` ` ` `x += (1 << j); ` ` ` `} ` ` ` ` ` `// Left shifting both numbers to ` ` ` `// traverse all the bits ` ` ` `A >>= 1; ` ` ` `B >>= 1; ` ` ` `j += 1; ` ` ` `} ` ` ` `return` `x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A = 2, B = 3; ` ` ` `int` `X = findX(A, B); ` ` ` ` ` `cout << ` `"X = "` `<< X << ` `", Sum = "` ` ` `<< (A ^ X) + (B ^ X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the integer X such that ` ` ` `// (A xor X) + (B ^ X) is minimized ` ` ` `static` `int` `findX(` `int` `A, ` `int` `B) ` ` ` `{ ` ` ` `int` `j = ` `0` `, x = ` `0` `; ` ` ` ` ` `// While either A or B is non-zero ` ` ` `while` `(A != ` `0` `|| B != ` `0` `) { ` ` ` ` ` `// Position at which both A and B ` ` ` `// have a set bit ` ` ` `if` `((A % ` `2` `== ` `1` `) && (B % ` `2` `== ` `1` `)) { ` ` ` ` ` `// Inserting a set bit in x ` ` ` `x += (` `1` `<< j); ` ` ` `} ` ` ` ` ` `// Left shifting both numbers to ` ` ` `// traverse all the bits ` ` ` `A >>= ` `1` `; ` ` ` `B >>= ` `1` `; ` ` ` `j += ` `1` `; ` ` ` `} ` ` ` `return` `x; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `A = ` `2` `, B = ` `3` `; ` ` ` `int` `X = findX(A, B); ` ` ` ` ` `System.out.println(` `"X = "` `+ X + ` `", Sum = "` ` ` `+ ((A ^ X) + (B ^ X))); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the integer X such that ` `# (A xor X) + (B ^ X) is minimized ` `def` `findX(A,B): ` ` ` `j ` `=` `0` ` ` `x ` `=` `0` ` ` ` ` `# While either A or B is non-zero ` ` ` `while` `(A ` `or` `B): ` ` ` ` ` `# Position at which both A and B ` ` ` `# have a set bit ` ` ` `if` `((A & ` `1` `) ` `and` `(B & ` `1` `)): ` ` ` ` ` `# Inserting a set bit in x ` ` ` `x ` `+` `=` `(` `1` `<< j) ` ` ` ` ` `# Left shifting both numbers to ` ` ` `# traverse all the bits ` ` ` `A >>` `=` `1` ` ` `B >>` `=` `1` ` ` `j ` `+` `=` `1` ` ` `return` `x ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `A ` `=` `2` ` ` `B ` `=` `3` ` ` `X ` `=` `findX(A, B) ` ` ` ` ` `print` `(` `"X ="` `,X,` `", Sum ="` `,(A ^ X) ` `+` `(B ^ X)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the integer X such that ` ` ` `// (A xor X) + (B ^ X) is minimized ` ` ` `static` `int` `findX(` `int` `A, ` `int` `B) ` ` ` `{ ` ` ` `int` `j = 0, x = 0; ` ` ` ` ` `// While either A or B is non-zero ` ` ` `while` `(A != 0 || B != 0) { ` ` ` ` ` `// Position at which both A and B ` ` ` `// have a set bit ` ` ` `if` `((A % 2 == 1) && (B % 2 == 1)) { ` ` ` ` ` `// Inserting a set bit in x ` ` ` `x += (1 << j); ` ` ` `} ` ` ` ` ` `// Left shifting both numbers to ` ` ` `// traverse all the bits ` ` ` `A >>= 1; ` ` ` `B >>= 1; ` ` ` `j += 1; ` ` ` `} ` ` ` `return` `x; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `A = 2, B = 3; ` ` ` `int` `X = findX(A, B); ` ` ` ` ` `Console.WriteLine(` `"X = "` `+ X + ` `", Sum = "` ` ` `+ ((A ^ X) + (B ^ X))); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the integer X such that ` `// (A xor X) + (B ^ X) is minimized ` `function` `findX(` `$A` `, ` `$B` `) ` `{ ` ` ` `$j` `= 0; ` ` ` `$x` `= 0; ` ` ` ` ` `// While either A or B is non-zero ` ` ` `while` `(` `$A` `|| ` `$B` `) { ` ` ` ` ` `// Position at which both A and B ` ` ` `// have a set bit ` ` ` `if` `((` `$A` `& 1) && (` `$B` `& 1)) ` ` ` `{ ` ` ` ` ` `// Inserting a set bit in x ` ` ` `$x` `+= (1 << ` `$j` `); ` ` ` `} ` ` ` ` ` `// Left shifting both numbers to ` ` ` `// traverse all the bits ` ` ` `$A` `>>= 1; ` ` ` `$B` `>>= 1; ` ` ` `$j` `+= 1; ` ` ` `} ` ` ` `return` `$x` `; ` `} ` ` ` `// Driver code ` ` ` `$A` `= 2; ` ` ` `$B` `= 3; ` ` ` `$X` `= findX(` `$A` `, ` `$B` `); ` ` ` ` ` `echo` `"X = "` `, ` `$X` `, ` `", Sum = "` `, ` ` ` `(` `$A` `^ ` `$X` `) + (` `$B` `^ ` `$X` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

X = 2, Sum = 1

## Recommended Posts:

- Find a point such that sum of the Manhattan distances is minimized
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Choose points from two ranges such that no point lies in both the ranges
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Find maximum value of the last element after reducing the array with given operations
- Find Nth smallest number that is divisible by 100 exactly K times
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Find the maximum number of elements divisible by 3
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- Find the Kth smallest element in the sorted generated array
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Count pairs with set bits sum equal to K
- Number of ways to get a given sum with n number of m-faced dices
- Print Lower Hessenberg matrix of order N

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.