Bit Difference | Practice | GeeksforGeeks

You are given two numbers A and B. The task is to count the number of bits needed to be flipped to convert A to B.

Example 1:
Input: A = 10, B = 20
Output: 4
Explanation:
A  = 01010
B  = 10100
As we can see, the bits of A that need 
to be flipped are 01010. If we flip 
these bits, we get 10100, which is B.

Example 2:
Input: A = 20, B = 25
Output: 3
Explanation:
A  = 10100
B  = 11001
As we can see, the bits of A that need 
to be flipped are 10100. If we flip 
these bits, we get 11001, which is B.


Your Task: The task is to complete the function countBitsFlip() that takes A and B as parameters and returns the count of the number of bits to be flipped to convert A to B.

Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ A, B ≤ 106
0
onlinecoursesgfg1 week ago
int countBitsFlip(int a, int b){              // Your logic here       int n = a ^ b;       int count = 0;       while(n > 0)       {           n = n & (n-1);           count++;       }       return count;   }
0
ninthale1 week ago
int countBitsFlip(int a, int b){             int c= a^b;      int d = setcount(c);      return d;          }   int setcount(int c)   {              if(c==0)       {           return 0;       }       else       {           return (c & 1) + setcount(c >> 1);       }   }
0
rohanmeher1641 week ago
class Solution{      public static int countBitsFlip(int a, int b){              int count=0;              while(a!=0 || b!=0)       {           if((a & 1)!=(b & 1))             {                count++;              }            a= a>>1;            b= b>>1;       }       return count;          }      } 
+1
gaurabhkumarjha271020011 week ago
// in c++ one line solution
   
   return __builtin_popcount(a^b);
        
0
sg6214281 week ago
class Solution{
    
    // Function to find number of bits needed to be flipped to convert A to B
    public static int countBitsFlip(int a, int b){
        int count=0;
        while(a!=0 || b!=0)
        {
            if((a&1)!=(b&1))
            count++;
            a>>=1;
            b>>=1;
        }
        return count;
    }
}
0
60mqcs20shivamyadav2 weeks ago
#Python code 
def countBitsFlip(self,a,b):       ##Your code here       count = 0       mask = a ^ b       while mask != 0:           mask = mask & mask -1           count +=1       return count
0
bharathimp19932 weeks ago
Java solution:
 
 public static int countBitsFlip(int a, int b){              // Your code here       int xor  = a^b;       int count=0;       while(xor!=0){           if((xor&1)==1){              count++;            }           xor = xor>>1;       }              return count;   }
0
mankesh0162 weeks ago
int n=a^b;
int cnt=1;
while(n&=n-1)
  cnt++;

return cnt;
0
mankesh016
This comment was deleted.
+1
bipulharsh1232 weeks ago
int countBitsFlip(int a, int b){              // Your logic here       int bita, bitb, c=1, to_flipped=0;       while(a || b){           bita = c&a;           bitb = c&b;           if(bita != bitb)               to_flipped++;           a>>=1;           b>>=1;       }       return to_flipped;          }
 
space : O(1)
time: O(logN)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.