Given three positive integers **X**, **Y** and **Z**, the task is to count the minimum numbers of bits required to be flipped in **X** and **Y** such that **X OR Y (X | Y)** is equal to **Z**.**Examples:**

Input :X = 5 Y = 8 Z = 6Output :3Explanation :BeforeAfterFlippingFlippingX :: 01010100Y ::10000010 ------ ----- Z :: 0110 0110 So we need to flip 3 bits in order to make (X | Y) = Z .Input :X = 1 Y = 2 Z = 3Output :0

**Approach :**

In order to solve this problem, we need to observe that the need to flip a bit X or Y arises only for the following conditions:

- If the current bit in Z is set and the corresponding bit in both X and Y is not set, we need to set a bit in either X or in Y.
- If the current bit in Z is unset, we need to unset the corresponding bit in A and B, whichever is set. Unset both if both are set.

Thus, we need to traverse through the bits of X, Y, and Z and follow the above two steps to get the desired result.

Below is the implementation of the above approach:

`// C++ Program to minimize` `// number of bits to be fipped ` `// in X or Y such that their ` `// bitwise or is equal to minimize` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// This function returns minimum ` `// number of bits to be flipped in` `// X and Y to make X | Y = Z` `int` `minimumFlips(` `int` `X, ` `int` `Y, ` `int` `Z)`
`{` ` ` `int` `res = 0;`
` ` `while` `(X > 0 || Y > 0 || Z > 0) {`
` ` ` ` `// If current bit in Z is set and is `
` ` `// also set in either of X or Y or both`
` ` `if` `(((X & 1) || (Y & 1)) && (Z & 1)) {`
` ` `X = X >> 1;`
` ` `Y = Y >> 1;`
` ` `Z = Z >> 1;`
` ` `continue` `;`
` ` `}`
` ` `// If current bit in Z is set `
` ` `// and is unset in both X and Y`
` ` `else` `if` `(!(X & 1) && !(Y & 1) && (Z & 1)) {`
` ` `// Set that bit in either X or Y`
` ` `res++;`
` ` `}`
` ` `else` `if` `((X & 1) || (Y & 1) == 1) `
` ` `{`
` ` `// If current bit in Z is unset `
` ` `// and is set in both X and Y`
` ` `if` `((X & 1) && (Y & 1) && !(Z & 1)) {`
` ` `// Unset the bit in both X and Y`
` ` `res += 2;`
` ` `}`
` ` `// If current bit in Z is unset and `
` ` `// is set in either X or Y `
` ` `else` `if` `(((X & 1) || (Y & 1)) && !(Z & 1)) `
` ` `{`
` ` `// Unset that set bit`
` ` `res++;`
` ` `}`
` ` `}`
` ` `X = X >> 1;`
` ` `Y = Y >> 1;`
` ` `Z = Z >> 1;`
` ` `}`
` ` `return` `res;`
`}` `// Driver Code` `int` `main()`
`{` ` ` `int` `X = 5, Y = 8, Z = 6;`
` ` `cout << minimumFlips(X, Y, Z);`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

`// Java program to minimize` `// number of bits to be fipped ` `// in X or Y such that their ` `// bitwise or is equal to minimize` `import` `java.util.*;`
`class` `GFG{`
`// This function returns minimum ` `// number of bits to be flipped in` `// X and Y to make X | Y = Z` `static` `int` `minimumFlips(` `int` `X, ` `int` `Y, ` `int` `Z)`
`{` ` ` `int` `res = ` `0` `;`
` ` `while` `(X > ` `0` `|| Y > ` `0` `|| Z > ` `0` `)`
` ` `{`
` ` ` ` `// If current bit in Z is set and is `
` ` `// also set in either of X or Y or both`
` ` `if` `(((X % ` `2` `== ` `1` `) || `
` ` `(Y % ` `2` `== ` `1` `)) && `
` ` `(Z % ` `2` `== ` `1` `))`
` ` `{`
` ` `X = X >> ` `1` `;`
` ` `Y = Y >> ` `1` `;`
` ` `Z = Z >> ` `1` `;`
` ` `continue` `;`
` ` `}`
` ` ` ` `// If current bit in Z is set `
` ` `// and is unset in both X and Y`
` ` `else` `if` `(!(X % ` `2` `== ` `1` `) &&`
` ` `!(Y % ` `2` `== ` `1` `) &&`
` ` `(Z % ` `2` `== ` `1` `)) `
` ` `{`
` ` ` ` `// Set that bit in either X or Y`
` ` `res++;`
` ` `}`
` ` `else` `if` `((X % ` `2` `== ` `1` `) || (Y % ` `2` `== ` `1` `)) `
` ` `{`
` ` ` ` `// If current bit in Z is unset `
` ` `// and is set in both X and Y`
` ` `if` `((X % ` `2` `== ` `1` `) && `
` ` `(Y % ` `2` `== ` `1` `) && `
` ` `!(Z % ` `2` `== ` `1` `))`
` ` `{`
` ` ` ` `// Unset the bit in both X and Y`
` ` `res += ` `2` `;`
` ` `}`
` ` ` ` `// If current bit in Z is unset and `
` ` `// is set in either X or Y `
` ` `else` `if` `(((X % ` `2` `== ` `1` `) || `
` ` `(Y % ` `2` `== ` `1` `)) && `
` ` `!(Z % ` `2` `== ` `1` `)) `
` ` `{`
` ` ` ` `// Unset that set bit`
` ` `res++;`
` ` `}`
` ` `}`
` ` `X = X >> ` `1` `;`
` ` `Y = Y >> ` `1` `;`
` ` `Z = Z >> ` `1` `;`
` ` `}`
` ` `return` `res;`
`}` `// Driver Code` `public` `static` `void` `main(String[] args)`
`{` ` ` `int` `X = ` `5` `, Y = ` `8` `, Z = ` `6` `;`
` ` ` ` `System.out.print(minimumFlips(X, Y, Z));`
`}` `}` `// This code is contributed by amal kumar choubey` |

*chevron_right*

*filter_none*

`# Python 3 Program to minimize` `# number of bits to be fipped` `# in X or Y such that their` `# bitwise or is equal to minimize` `# This function returns minimum` `# number of bits to be flipped in` `# X and Y to make X | Y = Z` `def` `minimumFlips(X, Y, Z):`
` ` `res ` `=` `0`
` ` `while` `(X > ` `0` `or` `Y > ` `0` `or`
` ` `Z > ` `0` `):`
` ` `# If current bit in Z is `
` ` `# set and is also set in `
` ` `# either of X or Y or both`
` ` `if` `(((X & ` `1` `) ` `or` `(Y & ` `1` `)) ` `and`
` ` `(Z & ` `1` `)):`
` ` `X ` `=` `X >> ` `1`
` ` `Y ` `=` `Y >> ` `1`
` ` `Z ` `=` `Z >> ` `1`
` ` `continue`
` ` `# If current bit in Z is set`
` ` `# and is unset in both X and Y`
` ` `elif` `(` `not` `(X & ` `1` `) ` `and` `not` `(Y & ` `1` `) ` `and`
` ` `(Z & ` `1` `)):`
` ` ` ` `# Set that bit in either `
` ` `# X or Y`
` ` `res ` `+` `=` `1`
` ` `elif` `((X & ` `1` `) ` `or` `(Y & ` `1` `) ` `=` `=` `1` `):`
` ` `# If current bit in Z is unset`
` ` `# and is set in both X and Y`
` ` `if` `((X & ` `1` `) ` `and` `(Y & ` `1` `) ` `and`
` ` `not` `(Z & ` `1` `)):`
` ` ` ` `# Unset the bit in both `
` ` `# X and Y`
` ` `res ` `+` `=` `2`
` ` `# If current bit in Z is `
` ` `# unset and is set in `
` ` `# either X or Y`
` ` `elif` `(((X & ` `1` `) ` `or` `(Y & ` `1` `)) ` `and`
` ` `not` `(Z & ` `1` `)):`
` ` `# Unset that set bit`
` ` `res ` `+` `=` `1`
` ` `X ` `=` `X >> ` `1`
` ` `Y ` `=` `Y >> ` `1`
` ` `Z ` `=` `Z >> ` `1`
` ` `return` `res`
`# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:`
` ` `X ` `=` `5`
` ` `Y ` `=` `8`
` ` `Z ` `=` `6`
` ` `print` `(minimumFlips(X, Y, Z))`
`# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

`// C# program to minimize` `// number of bits to be fipped ` `// in X or Y such that their ` `// bitwise or is equal to minimize` `using` `System;`
`class` `GFG{`
`// This function returns minimum ` `// number of bits to be flipped in` `// X and Y to make X | Y = Z` `static` `int` `minimumFlips(` `int` `X, ` `int` `Y, ` `int` `Z)`
`{` ` ` `int` `res = 0;`
` ` `while` `(X > 0 || Y > 0 || Z > 0)`
` ` `{`
` ` ` ` `// If current bit in Z is set and is `
` ` `// also set in either of X or Y or both`
` ` `if` `(((X % 2 == 1) || `
` ` `(Y % 2 == 1)) && `
` ` `(Z % 2 == 1))`
` ` `{`
` ` `X = X >> 1;`
` ` `Y = Y >> 1;`
` ` `Z = Z >> 1;`
` ` `continue` `;`
` ` `}`
` ` ` ` `// If current bit in Z is set `
` ` `// and is unset in both X and Y`
` ` `else` `if` `(!(X % 2 == 1) &&`
` ` `!(Y % 2 == 1) &&`
` ` `(Z % 2 == 1)) `
` ` `{`
` ` ` ` `// Set that bit in either X or Y`
` ` `res++;`
` ` `}`
` ` `else` `if` `((X % 2 == 1) || (Y % 2 == 1)) `
` ` `{`
` ` ` ` `// If current bit in Z is unset `
` ` `// and is set in both X and Y`
` ` `if` `((X % 2 == 1) && `
` ` `(Y % 2 == 1) && `
` ` `!(Z % 2 == 1))`
` ` `{`
` ` ` ` `// Unset the bit in both X and Y`
` ` `res += 2;`
` ` `}`
` ` ` ` `// If current bit in Z is unset and `
` ` `// is set in either X or Y `
` ` `else` `if` `(((X % 2 == 1) || `
` ` `(Y % 2 == 1)) && `
` ` `!(Z % 2 == 1)) `
` ` `{`
` ` ` ` `// Unset that set bit`
` ` `res++;`
` ` `}`
` ` `}`
` ` `X = X >> 1;`
` ` `Y = Y >> 1;`
` ` `Z = Z >> 1;`
` ` `}`
` ` `return` `res;`
`}` `// Driver Code` `public` `static` `void` `Main()`
`{` ` ` `int` `X = 5, Y = 8, Z = 6;`
` ` ` ` `Console.Write(minimumFlips(X, Y, Z));`
`}` `}` `// This code is contributed by Code_Mech` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- 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 total bits toggled/flipped in binary representation of 0 to N
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Count minimum bits to flip such that XOR of A and B equal to C

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.