Given two positive integers A and B, the task is to flip the common set bitsin A and B.
Examples:
Input: A = 7, B = 4
Output: 3 0
Explanation:
The binary representation of 7 is 111
The binary representation of 4 is 100
Since the 3rd bit of both A and B is a set bit. Therefore, flipping the 3rd bit of A and B modifies A = 3 and B = 0
Therefore, the required output is 3 0Input: A = 10, B = 20
Output: 10 20
Naive Approach: The simplest approach to solve this problem is to check if the ith bit of both A and B is a set or not. If found to be true, then flip the ith bit of both A and B. Finally, print the updated values of both A and B.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to flip bits of A and B// which are set bits in A and Bvoid flipBitsOfAandB(int A, int B){ // Iterater all possible bits // of A and B for (int i = 0; i < 32; i++) { // If ith bit is set in // both A and B if ((A & (1 << i)) && (B & (1 << i))) { // Clear i-th bit of A A = A ^ (1 << i); // Clear i-th bit of B B = B ^ (1 << i); } } // Print A and B cout << A << " " << B;}// Driver Codeint main(){ int A = 7, B = 4; flipBitsOfAandB(A, B); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to flip bits of A and B// which are set bits in A and Bstatic void flipBitsOfAandB(int A, int B){ // Iterater all possible bits // of A and B for(int i = 0; i < 32; i++) { // If ith bit is set in // both A and B if (((A & (1 << i)) & (B & (1 << i))) != 0) { // Clear i-th bit of A A = A ^ (1 << i); // Clear i-th bit of B B = B ^ (1 << i); } } // Print A and B System.out.print(A + " " + B);} // Driver Codepublic static void main(String[] args){ int A = 7, B = 4; flipBitsOfAandB(A, B);}}// This code is contributed by code_hunt |
Python3
# Python3 program to implement# the above approach# Function to flip bits of A and B# which are set in both of themdef flipBitsOfAandB(A, B): # Iterate all possible bits of # A and B for i in range(0, 32): # If ith bit is set in # both A and B if ((A & (1 << i)) and (B & (1 << i))): # Clear i-th bit of A A = A ^ (1 << i) # Clear i-th bit of B B = B ^ (1 << i) print(A, B)# Driver Code if __name__ == "__main__" : A = 7 B = 4 flipBitsOfAandB(A, B) # This code is contributed by Virusbuddah_ |
C#
// C# program to implement// the above approachusing System;class GFG{// Function to flip bits of A and B// which are set bits in A and Bstatic void flipBitsOfAandB(int A, int B){ // Iterater all possible bits // of A and B for(int i = 0; i < 32; i++) { // If ith bit is set in // both A and B if (((A & (1 << i)) & (B & (1 << i))) != 0) { // Clear i-th bit of A A = A ^ (1 << i); // Clear i-th bit of B B = B ^ (1 << i); } } // Print A and B Console.Write(A + " " + B);}// Driver Codepublic static void Main(string[] args){ int A = 7, B = 4; flipBitsOfAandB(A, B);}}// This code is contributed by chitranayal |
Javascript
<script>// javascript program to implement// the above approach// Function to flip bits of A and B// which are set bits in A and Bfunction flipBitsOfAandB(A , B){ // Iterater all possible bits // of A and B for(i = 0; i < 32; i++) { // If ith bit is set in // both A and B if (((A & (1 << i)) & (B & (1 << i))) != 0) { // Clear i-th bit of A A = A ^ (1 << i); // Clear i-th bit of B B = B ^ (1 << i); } } // Print A and B document.write(A + " " + B);} // Driver Codevar A = 7, B = 4;flipBitsOfAandB(A, B);// This code is contributed by Princi Singh</script> |
3 0
Time Complexity: O(32)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
Find the bits which are set bits in both A and B using (A & B).
Clear the bits of A which are set bits in both A and B using A = (A ^ (A & B))
Cleat the bits of B which are set bits in both A and B using B = (B ^ (A & B))
Follow the steps below to solve the problem:
- Update A = (A ^ (A & B)).
- Update B = (B ^ (A & B)).
- Finally, print the updated values of A and B.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to flip bits of A and B// which are set in both of themvoid flipBitsOfAandB(int A, int B){ // Clear the bits of A which // are set in both A and B A = A ^ (A & B); // Clear the bits of B which // are set in both A and B B = B ^ (A & B); // Print updated A and B cout << A << " " << B;}// Driver Codeint main(){ int A = 10, B = 20; flipBitsOfAandB(A, B); return 0;} |
Java
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to flip bits of A and B// which are set in both of themstatic void flipBitsOfAandB(int A, int B){ // Clear the bits of A which // are set in both A and B A = A ^ (A & B); // Clear the bits of B which // are set in both A and B B = B ^ (A & B); // Print updated A and B System.out.print(A + " " + B);} // Driver Codepublic static void main(String[] args){ int A = 10, B = 20; flipBitsOfAandB(A, B);}}// This code is contributed by sanjoy_62 |
Python3
# Python3 program to implement# the above approach# Function to flip bits of A and B# which are set in both of themdef flipBitsOfAandB(A, B): # Clear the bits of A which # are set in both A and B A = A ^ (A & B) # Clear the bits of B which # are set in both A and B B = B ^ (A & B) # Print updated A and B print(A, B)# Driver Code if __name__ == "__main__" : A = 10 B = 20 flipBitsOfAandB(A, B) # This code is contributed by Virusbuddah_ |
C#
// C# program to implement// the above approachusing System;class GFG{ // Function to flip bits of A and B// which are set in both of themstatic void flipBitsOfAandB(int A, int B){ // Clear the bits of A which // are set in both A and B A = A ^ (A & B); // Clear the bits of B which // are set in both A and B B = B ^ (A & B); // Print updated A and B Console.Write(A + " " + B);} // Driver Codepublic static void Main(String[] args){ int A = 10, B = 20; flipBitsOfAandB(A, B);}}// This code is contributed by Amit Katiyar |
Javascript
<script>// javascript program to implement// the above approach // Function to flip bits of A and B// which are set in both of themfunction flipBitsOfAandB(A , B){ // Clear the bits of A which // are set in both A and B A = A ^ (A & B); // Clear the bits of B which // are set in both A and B B = B ^ (A & B); // Print updated A and B document.write(A + " " + B);} // Driver Codevar A = 10, B = 20;flipBitsOfAandB(A, B);// This code contributed by shikhasingrajput</script> |
10 20
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


