Skip to content
Related Articles

Related Articles

Improve Article

Minimum bitwise operations to convert given a into b.

  • Difficulty Level : Hard
  • Last Updated : 22 Sep, 2017

Given two positive integer a and b you have to change a to b by applying any of the three operations on binary form of a. You can select ai and aj (any two bits where i!=j) from binary form of a and then perform operation as:

  • AND operation as : temp = ai & aj, ai = temp & ai, aj = temp & aj
  • OR operation as : temp = ai | aj, ai = temp | ai, aj = temp | aj
  • XOR operation as : temp = ai ^ aj, ai = temp ^ ai, aj = temp ^ aj

where & = bitwise AND, | = bitwiese OR and ^ = bitwise XOR.
Find the minimum operation required for conversion of a to b. Also, if conversion of a to b is not possible then print -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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:



Input : a = 12 (1100), b = 10 (1010)
Output : 1
Explanation : select a2 and a3 and perform XOR 

Input : a = 15 (1111), b = 10 (1010)
Output : -1
Explanation : Conversion from a to b is not possible

Explanation : First of all let’s understand the working of all three operation.

  1. AND operation as : temp = ai & aj, ai = temp & ai, aj = temp & aj

    If any of ai or aj is 0 then it makes both as 0 otherwise no effect on ai and aj.

  2. OR operation as : temp = ai | aj, ai = temp | ai, aj = temp | aj

    If any of ai or aj is 1 then it makes both as 1 otherwise no effect on ai and aj.

  3. XOR operation as : temp = ai ^ aj, ai = temp ^ ai, aj = temp ^ aj

    Simply interchange value of ai and aj.

Some conclusion on basis of working of operations :

  1. If all bits of a are 1 or 0 then we can not change value of a.
  2. If a equals to b then no operation required.
  3. Let n be number of indices i, where ai = 0 and bi = 1.
    Let m be number of indices i, where ai = 1 and bi = 0.

    Let us think about the n elements, where ai = 0 and bi = 1. We have to change all of these zeros into ones. Note that this will require at least n operations.
    Similarly for all the m elements, where ai = 1 and bi = 0. We have to change all of these ones into zeros. Note that this will require at least m operations.

    Let res = max(n, m). We can make the a an b equal in res operations as follows.

    Let n >= m. Take m 1’s and n 0’s in A and apply the XOR operation to swap 0’s with 1’s. After that you will be left with total n-m zeros elements to change to one. That you can do by taking each of these zeros with some single one and applying the OR operation.
    Let m >= n. Take m 1’s and n 0’s in A and apply the XOR operation to swap 0’s with 1’s. After that you will be left with total m-n ones elements to change to zero. That you can do by taking each of these ones with some single zero and applying the OR operation.




// Cpp program to find min operation to convert a to b
#include <bits/stdc++.h>
using namespace std;
  
// function to return min operation
int minOp(bitset<32> a1, bitset<32> b1)
{
    // if a1 == b1 return 0
    if (a1 == b1)
        return 0;
  
    // if all bits of a = 0
    if (a1 == 0)
        return -1;
  
    // if all bits of a =1
    // first convert a1 to int and then cal a1 & a1+1
    if (((int)a1.to_ulong() & ((int)a1.to_ulong() + 1)) 
                                               == 0)
        return -1;
  
    // convert a and b to binary string
    string a = a1.to_string();
    string b = b1.to_string();
  
    // check where ai and bi are different
    // and count n where ai = 1 and m where ai = 0
    int n = 0, m = 0;
    for (int i = 0; i < b.size(); i++) {
        if (b[i] != a[i]) {
            if (a[i] == '1')
                n++;
            else
                m++;
        }
    }
  
    // return result
    return max(n, m);
}
  
// driver program
int main()
{
    bitset<32> a = 14, b = 1;
    cout << minOp(a, b);
    return 0;
}

Output:

3

This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :