VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-bitwise-operations-to-convert-given-a-into-b/

⇱ Minimum bitwise operations to convert given a into b. - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum bitwise operations to convert given a into b.

Last Updated : 28 Mar, 2023

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, | = bitwise 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.
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 and 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.


 


 

Output: 
 

3

Time Complexity - O(K)

Space Complexity - O(K)

Here, K is a constant.


 

Comment
Article Tags:
Article Tags: