Count number of bits to be flipped to convert A to B | Set-2 - GeeksforGeeks

17 Nov, 2021
Given two integers A and B, the task is to count the number of bits needed to be flipped to convert A to B.Examples:  
Input: A = 10, B = 7 Output: 3 binary(10) = 1010 binary(7) = 0111 1010 0111 3 bits need to be flipped.Input: A = 8, B = 7 Output: 4  
 
Approach: An approach to solve this problem has already been discussed here. Here, the count of bits that need to be flipped can be found by matching all the bits in both the integers one by one. If the bit under consideration differs then increment the count.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of bits// to be flipped to convert a to bint countBits(int a, int b){     // To store the required count    int count = 0;     // Loop until both of them become zero    while (a || b) {         // Store the last bits in a        // as well as b        int last_bit_a = a & 1;        int last_bit_b = b & 1;         // If the current bit is not same        // in both the integers        if (last_bit_a != last_bit_b)            count++;         // Right shift both the integers by 1        a = a >> 1;        b = b >> 1;    }     // Return the count    return count;} // Driver codeint main(){    int a = 10, b = 7;     cout << countBits(a, b);     return 0;}
// Java implementation of the approachimport java.util.*;class GFG{ // Function to return the count of bits// to be flipped to convert a to bstatic int countBits(int a, int b){     // To store the required count    int count = 0;     // Loop until both of them become zero    while (a > 0 || b > 0)    {         // Store the last bits in a        // as well as b        int last_bit_a = a & 1;        int last_bit_b = b & 1;         // If the current bit is not same        // in both the integers        if (last_bit_a != last_bit_b)            count++;         // Right shift both the integers by 1        a = a >> 1;        b = b >> 1;    }     // Return the count    return count;} // Driver codepublic static void main(String[] args){    int a = 10, b = 7;     System.out.println(countBits(a, b));}} // This code is contributed by Princi Singh
# Python3 implementation of the approach # Function to return the count of bits# to be flipped to convert a to bdef countBits(a, b):     # To store the required count    count = 0     # Loop until both of them become zero    while (a or b):         # Store the last bits in a        # as well as b        last_bit_a = a & 1        last_bit_b = b & 1         # If the current bit is not same        # in both the integers        if (last_bit_a != last_bit_b):            count += 1         # Right shift both the integers by 1        a = a >> 1        b = b >> 1     # Return the count    return count # Driver codea = 10b = 7 print(countBits(a, b)) # This code is contributed by Mohit Kumar
// C# implementation of the above approachusing System; class GFG{ // Function to return the count of bits// to be flipped to convert a to bstatic int countBits(int a, int b){     // To store the required count    int count = 0;     // Loop until both of them become zero    while (a > 0 || b > 0)    {         // Store the last bits in a        // as well as b        int last_bit_a = a & 1;        int last_bit_b = b & 1;         // If the current bit is not same        // in both the integers        if (last_bit_a != last_bit_b)            count++;         // Right shift both the integers by 1        a = a >> 1;        b = b >> 1;    }     // Return the count    return count;} // Driver codepublic static void Main(String[] args){    int a = 10, b = 7;     Console.WriteLine(countBits(a, b));}} // This code is contributed by PrinciRaj1992
<script> // Javascript implementation of the approach // Function to return the count of bits// to be flipped to convert a to bfunction countBits(a, b){     // To store the required count    var count = 0;     // Loop until both of them become zero    while (a || b) {         // Store the last bits in a        // as well as b        var last_bit_a = a & 1;        var last_bit_b = b & 1;         // If the current bit is not same        // in both the integers        if (last_bit_a != last_bit_b)            count++;         // Right shift both the integers by 1        a = a >> 1;        b = b >> 1;    }     // Return the count    return count;} // Driver code    var a = 10, b = 7;    document.write(countBits(a, b)); </script>
3
 
Time Complexity: O(min(log a, log b))
Auxiliary Space: O(1)
mohit kumar 29
princi singh
princiraj1992
bgangwar59
subhammahato348
Numbers
Bit Magic
Bit Magic
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Set, Clear and Toggle a given bit of a number in C
Program to find parity
Hamming code Implementation in C/C++
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Implementation of Bit Stuffing and Bit Destuffing
Builtin functions of GCC compiler
Find XOR of all elements in an Array
Swap bits in a given number
Count total bits in a number